I'm working with a java application and trying to modify this code so that it no longer prompts the user with the Dialog box to save a txt file. The user already uses the Dialog box to choose a directory in which to save the file at an early point in the application. So I have the JTextField path in a different class. I would like this code to call the path from the other class and then save the txt file.
//Choose where to save it, then send path to xml writer.
JFileChooser filesave = new JFileChooser();
FileFilter filter = new FileNameExtensionFilter("Text file", "txt");
filesave.addChoosableFileFilter(filter);
int ret = filesave.showSaveDialog(null);
//Valid file loaded - create file
FileWriter fstream = null;
if (ret == JFileChooser.APPROVE_OPTION) {
if (filesave.getSelectedFile().getName().contains(".txt")) {
fstream = new FileWriter(filesave.getSelectedFile().getPath());
} else {
fstream = new FileWriter(filesave.getSelectedFile().getPath() + ".txt");
}
} else {
JOptionPane.showMessageDialog(i.getFrame(), "An error occured choosing the save file. Please try again.", "Saving File Error", JOptionPane.WARNING_MESSAGE);
}
BufferedWriter2 out = new BufferedWriter2(fstream);
//Begin writing the file
Any help is greatly appreciated!!!!
Thanks