Results 1 to 3 of 3
  1. #1
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default 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:
    Java Code:
    private void jMenuItem2ActionPerformed(java.awt.event.ActionEvent evt) {                                           
            try {
                handler.saveProject();
            } catch (FileNotFoundException e) {
                e.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    handler is a SurviveProjectHandler.

    Here's the code for the saveProject() method.

    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());
           }
    The project pathName and fileName are set using this:

    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());
    Sorry I could not format the last code block right.

    Thank you in advance!

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,882
    Rep Power
    8

    Default

    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.

  3. #3
    Join Date
    Nov 2010
    Posts
    2
    Rep Power
    0

    Default

    In this case, the absolute path doesn't include the filename, though I think that it should. The problem is I don't know how to go about that.

Similar Threads

  1. [SOLVED] jfilechooser for saving a file....?
    By prabhurangan in forum AWT / Swing
    Replies: 10
    Last Post: 10-06-2013, 10:17 PM
  2. Saving to file from JFileChooser SaveDialog
    By kieran82 in forum AWT / Swing
    Replies: 2
    Last Post: 03-16-2010, 03:09 AM
  3. JFileChooser problem
    By jperson in forum New To Java
    Replies: 2
    Last Post: 02-06-2010, 07:31 PM
  4. jfilechooser and jtextarea problem
    By kostinio in forum AWT / Swing
    Replies: 7
    Last Post: 10-07-2009, 08:46 AM
  5. JFileChooser horizontal scrollbar problem
    By nenadm in forum AWT / Swing
    Replies: 4
    Last Post: 11-12-2008, 11:16 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •