Results 1 to 3 of 3
- 11-24-2010, 05:24 PM #1
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Problem with saving from JFileChooser
Hello,
Whenever I try to create a text file using a filename selected with JFileChooser, it comes up with java.io.FileNotFoundException: /home/johan/hello/hello.sproj (Is a directory). Here's the code:
MainWindow.java:
handler is a SurviveProjectHandler.Java Code:private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) { try { handler.saveProject(); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } }
Here's the code for the saveProject() method.
The project pathName and fileName are set using this:Java Code:public void saveProject() throws FileNotFoundException, IOException { ... File f = new File(project.getPathName(), project.getFileName()); if (!f.exists()) { f.mkdirs(); f.createNewFile(); } if (f.exists()) { f.delete(); f.mkdirs(); f.createNewFile(); } if (f.canWrite()) { PrintWriter pw = new PrintWriter(new FileWriter(f)); pw.println(project.getTitle()); }
Sorry I could not format the last code block right.Java Code:JFileChooser chooser = new JFileChooser(); FileNameExtensionFilter filter = new FileNameExtensionFilter("Survive Projects (*." + SurviveConstants.PROJECT_FILE_EXTENSION + ")", "sproj"); chooser.setFileFilter(filter); int returnVal = chooser.showSaveDialog(this); if (returnVal == JFileChooser.APPROVE_OPTION) { System.out.println("Filename: " + chooser.getSelectedFile().getAbsolutePath()); File f = chooser.getSelectedFile(); handler.setPathName(f.getAbsolutePath()); handler.setFileName(f.getName());
Thank you in advance!
- 11-24-2010, 05:30 PM #2
Why do you pass in both the absolute path of the file (which includes the file name, no?) and the name of the file? Isn't that redundant?
But without an SSCCE, I'm only guessing.
- 11-24-2010, 08:21 PM #3
Member
- Join Date
- Nov 2010
- Posts
- 2
- Rep Power
- 0
Similar Threads
-
[SOLVED] jfilechooser for saving a file....?
By prabhurangan in forum AWT / SwingReplies: 9Last Post: 03-19-2012, 05:20 PM -
Saving to file from JFileChooser SaveDialog
By kieran82 in forum AWT / SwingReplies: 2Last Post: 03-16-2010, 03:09 AM -
JFileChooser problem
By jperson in forum New To JavaReplies: 2Last Post: 02-06-2010, 07:31 PM -
jfilechooser and jtextarea problem
By kostinio in forum AWT / SwingReplies: 7Last Post: 10-07-2009, 08:46 AM -
JFileChooser horizontal scrollbar problem
By nenadm in forum AWT / SwingReplies: 4Last Post: 11-12-2008, 11:16 AM


LinkBack URL
About LinkBacks
Reply With Quote
Bookmarks