Results 1 to 5 of 5
  1. #1
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default [SOLVED]FileChooser checking for an existing file

    Hi everyone,

    I was taking a look at the Java Swing Tutorial and I implemented a FileChooser for saving files. However, I didn't find how to check if the file you are trying to save already exists in the directory. Is there a way to do it?
    Last edited by sky; 12-07-2009 at 12:29 PM.

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    I didn't realize that the filechooser only returns the file object, but it doesn't actually save it.

  4. #4
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

    Default

    Quote Originally Posted by sky View Post
    I didn't realize that the filechooser only returns the file object, but it doesn't actually save it.
    That is correct. You have to write the code to do that. If you haven't done so, have a look at the JFileChooser tutorial in the Sun Swing tutorials.

  5. #5
    sky
    sky is offline Member
    Join Date
    Nov 2009
    Posts
    96
    Rep Power
    0

    Default

    Yes, I've already had the code for that. For creating a FileChooser to save a file and check if the file exists, the code would be something like that (just in case somebody needs it):

    Java Code:
    JFileChooser fc = new JFileChooser();
    int returnVal = fc.showSaveDialog(frame);
      
    if (returnVal == JFileChooser.APPROVE_OPTION){
           File file = fc.getSelectedFile();
                
           if (file.exists()) {
                    int n = JOptionPane.showConfirmDialog(frame, "The given file does already exists \n Would you like to replace it?", "File found error", JOptionPane.YES_NO_OPTION);
                } 
                
                if (n==JOptionPane.YES_OPTION) {
                    String fileName = file.getName();
                    // Save the file
                 } else if n==JOptionPane.NO_OPTION) {
                    // Do nothing
                 } 
            } else {
                   // Save the file
            }
    }
    Last edited by sky; 12-07-2009 at 12:08 AM.

Similar Threads

  1. checking file extensions
    By gisler in forum New To Java
    Replies: 3
    Last Post: 12-05-2009, 02:25 PM
  2. Changing Existing Zip file using java
    By narayanan.1985 in forum Advanced Java
    Replies: 1
    Last Post: 09-30-2009, 09:27 PM
  3. Checking if file exists in a directory
    By achilles in forum New To Java
    Replies: 7
    Last Post: 08-10-2009, 03:43 AM
  4. show only PNG file in FileChooser
    By rabbia in forum New To Java
    Replies: 3
    Last Post: 02-25-2009, 09:04 AM
  5. Run J2ME application using existing jar file
    By chale in forum CLDC and MIDP
    Replies: 0
    Last Post: 08-25-2008, 03:11 PM

Posting Permissions

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