tags:

views:

254

answers:

3

I have a String that holds a path to a file. I want the users to be able to select a path and filename with a file chooser and have the program save the file given in the String variable to a location of their choice. What is the best way to do this?

+2  A: 

use JFileChooser

ufukgun
+4  A: 

Read the JFileChooser tutorial.

Bombe
+2  A: 

JFileChooser can provide the UI for selecting a file. See the Javadoc for documentation and example code.

You may then save the string to a file like this:

String stringToSave = "this will be saved...";
// set up the jfilechooser...
//
int returnVal = chooser.showSaveDialog(parent);
if(returnVal == JFileChooser.APPROVE_OPTION) {
    File file = chooser.getSelectedFile();
    try {
        PrintStream ps = new PrintStream(file);
        ps.print(stringToSave);
        ps.close();
    } catch (IOException ioe) {
        // ... handle errors!
    }
}
VoidPointer