简体   繁体   中英

Using BufferedWriter with JFileChooser to set save directory

For my ongoing project at work I have been asked to add a feature to my program. Currently the program allows a user to upload a file which is then manipulated. After being reformatted the file is saved into a new .ACH file (which is just a text file with strict formatting standards).

Currently the user is able to upload an ACH file from anywhere on their computer using this code:

 JFileChooser chooser = new JFileChooser();
        FileNameExtensionFilter filter = new FileNameExtensionFilter("ACH Files", "ach");
        chooser.setFileFilter(filter);
        chooser.setDialogTitle("Please choose ACH file to upload");
        int returnVal = chooser.showOpenDialog(chooser);
        chooser.setCurrentDirectory(null);
        if(returnVal == JFileChooser.APPROVE_OPTION)
        {

Later in the program buffered reader/writer make edits to the file and then save it to the new location using this code:

br = new BufferedReader(new FileReader(chooser.getSelectedFile()));
bw = new BufferedWriter(new FileWriter(chooser.getCurrentDirectory()+"//NachaOutput.ACH"));

Finally, the user is informed of the file name and save location using this code:

JOptionPane.showMessageDialog(null, "Output file saved as NachaOutput.ach to " + chooser.getCurrentDirectory());

As you can see, the new file is saved to the same directory that the file was uploaded from. My manager has requested that I allow the user the choice of a directory to save the new file to. (The file name is not imporant nor does it need to be set by the user, only the directory).

I have tried and tried to research how to do this, to no avail. The only guide that I found was very lengthy and I'll confess was too complicated for me to understand. I have attempted to create a new JFileChooser and allow the user to set the directory only and use the code:

int saveVal = chooser2.showSaveDialog(chooser2);

However I find that when I attempt to pull the saved directory later like this:

bw = new BufferedWriter(new FileWriter(chooser2.getCurrentDirectory()+"//NachaOutput.ACH"

the file is saved to my computers standard directory. (C://Users or something to that affect.)

Is there a basic way that I can give the user a simple input to select a directory and then utilize it with the bw on the line I shown above?

Thanks in advance.

Is there a basic way that I can give the user a simple input to select a directory

The basic way to do this is:

JFileChooser chooser = new JFileChooser();
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);

The rest maybe not that obvious (you have to ask for the chosen file to get the directory):

int returnVal = chooser.showDialog(chooser, "Directory to save");
if (returnVal == JFileChooser.APPROVE_OPTION) {
    System.out.println(chooser.getSelectedFile());
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM