I want to save my file in user selected location in java swing application. I am generating word file dynamically. Moreover When I am putting like this
FileWriter _file = new FileWriter("C:/Calender"+.doc", true);
the file is getting generated in c:/ drive directly in the name of Calender.doc.
However If i am passing path through
jfilechooser.getSelectedFile().getAbsolutePath()
returning the value "C:\\Users\\Shorav\\Desktop"
is not saving any file in this location.
Please help; how to save the file.
Code is
chooser = new JFileChooser();
chooser.setCurrentDirectory(new java.io.File("."));
chooser.setDialogTitle("Select location to save the file");
chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY);
chooser.setAcceptAllFileFilterUsed(false);
if (chooser.showOpenDialog(null) == JFileChooser.APPROVE_OPTION) {
// System.out.println("getCurrentDirectory(): "+ chooser.getCurrentDirectory());
// System.out.println("getSelectedFile() : "+ chooser.getSelectedFile());
System.out.println("Absolute Path : " +chooser.getSelectedFile().getAbsolutePath());
} else {
System.out.println("No Selection ");
}
This will ask from User for desired location to save the file.
Then this is returning the path by C:\\Users\\Shorav\\Desktop
. I want to generate the word file on this location through the following
FileWriter _file = new FileWriter("C:/Calender"+.doc", true);
This is returning word file.
You will get location of a folder user selected with jfilechooser.getSelectedFile
. Use it to construct a File
object and pass it to FileWriter
.
File selectedFile = jfilechooser.getSelectedFile();
FileWriter _file = new FileWriter(new File(selectedFile, "Calender.doc"), true);
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.