The Artima Developer Community
Sponsored Link

Java Answers Forum
Save With a given Path

2 replies on 1 page. Most recent reply: Oct 14, 2005 5:48 AM by Collin Garymore

Welcome Guest
  Sign In

Go back to the topic listing  Back to Topic List Click to reply to this topic  Reply to this Topic Click to search messages in this forum  Search Forum Click for a threaded view of the topic  Threaded View   
Previous Topic   Next Topic
Flat View: This topic has 2 replies on 1 page
Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Save With a given Path Posted: Oct 12, 2005 3:45 AM
Reply to this message Reply
Advertisement
Hi everyone,
I wonder if someone can help me with this problem.
I create a folder, and would like to save files inside this folder.
Could someone tell me which argument to utilise for this purpose,
Many thanks.
1. This one does the creation
/**
 *
 */
fileChooser = new JFileChooser ();
cwd = fileChooser.getCurrentDirectory ();
if ( cwd !=null )
{
    new_Dir = new File ( cwd, "RSA_KeyPair" );
    new_Dir.mkdir ();
}
 
/**
 *
 */
performedSaveFiles ( userName, priv_Key, pub_Key );


2. That one does the saving
/*
 *
 */
private void performedSaveFiles ( String passed_Name,
				  String passed_PrivKey,
				  String passed_PubKey )
{
    /**
     * Returns the selected file.
     * @param name
     */
    File name = new File ( passed_Name );
 
    try {
       /**
        *
        */
       currentDir = new File ( "C:\\Documents and Settings\\Collin Galagher\\My Documents\\RSA_KeyPair" );

Were the Problem Start
     
        /**There is the Troubled case...:=)**/
        fileChooser.setCurrentDirectory ( currentDir );
 
	if ( currentDir !=null )
	{
            /**
	     * Constructs a FileWriter object given a File object.
	     * @param fileWriter_PrivKey
	     * @param name - a File object to write to.
	     */
	     FileWriter fileWriter_PrivKey = new FileWriter ( name+".privKey" );
 
	     fileWriter_PrivKey.write ( priv_Key );
 
	     /**
	      *
	      * @param fileWriter_PubKey -
	      */
	      FileWriter fileWriter_PubKey = new FileWriter ( name+".pubKey" );
 
	      fileWriter_PubKey.write ( pub_Key );
 
	      /**
	       * @param close (). Close the stream.
	       */
	      fileWriter_PrivKey.close ();
	      fileWriter_PubKey.close ();
	}
 
	/**
	 * Setup the Hint bar
	 * @param hintBar
	 */
	RSA_Demo.hintBar.setText ( " The Files: "+name+".PrivKey and " +name+".PubKey are now saved " );
    } catch ( FileNotFoundException fnfex ) {
            /**
             * Returns the default toolkit.
             * param beep ()
             */
            Toolkit.getDefaultToolkit ().beep ();
 
            JOptionPane.showMessageDialog ( null,
                " File does not Exist" +
                " Message is: [" + fnfex.getMessage () +"]",
                " Invalid File Name",
                    JOptionPane.ERROR_MESSAGE );
    } catch ( IOException ioex ) {
       /**
        * Returns the default toolkit.
        * param beep ()
        */
       Toolkit.getDefaultToolkit ().beep ();
 
       JOptionPane.showMessageDialog ( null,
           " This File cannot be Write" +
           " Message is: [" + ioex.getMessage () +"]",
           " File Cannot be Write",
              JOptionPane.ERROR_MESSAGE );
    }
}   // End for Method performedSaveFiles


Matthias Neumair

Posts: 660
Nickname: neumi
Registered: Sep, 2003

Re: Save With a given Path Posted: Oct 12, 2005 4:39 AM
Reply to this message Reply
What error messages do you get?

Writing to a file is simple.


    public static void saveToFile (String fileName, String[] fileContent, boolean append, boolean doOutput) {
        if (fileName == null)
            return;
        saveToFile(new File(fileName), fileContent, append, doOutput);
    }
    public static void saveToFile (File target, String[] fileContent, boolean append, boolean doOutput) {
        if (fileContent == null || target == null || target.getParentFile() == null || !target.getParentFile().exists())
            return;
        try {
            PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter(target, append)));
            if (doOutput)
                System.out.print("Writing ");
            for (String line : fileContent) {
                out.println(line);
                if (doOutput)
                    System.out.print(".");
            }
            if (doOutput)
                System.out.println (" done");
            out.close();
        } catch (IOException e) {
            System.out.println("I/O Error: " + e.getMessage());
        }
    }


A binary file should work in a similar way.

Collin Garymore

Posts: 22
Nickname: callan
Registered: Jan, 2005

Re: Save With a given Path Posted: Oct 14, 2005 5:48 AM
Reply to this message Reply
I would like Matthias Neumair to thanks you for your time thus,
I will implement your solution.

Regarding:
What error messages do you get?

There were no messages in output.
No Files were added to the new created Directory-only.
I'll give a go to your solution this week end...
Once not last, many thanks.

Collin.

Flat View: This topic has 2 replies on 1 page
Topic: java.lang.OutOfMemoryError Previous Topic   Next Topic Topic: want to create access db from java using jdbc

Sponsored Links



Google
  Web Artima.com   

Copyright © 1996-2019 Artima, Inc. All Rights Reserved. - Privacy Policy - Terms of Use