Results 1 to 7 of 7
  1. #1
    Ballister is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default Exist or not exist file problem

    Hi there.

    So, i have some text writeen on a textpane and now i want to save it as a document but my problem is how to know if the file is already saved or not on the disc.

    If the file is already on it i just want to "update" it and if it's not i want to create the file and put the text there...

    Can anyone give me some tips to solve this ?

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

  3. #3
    Ballister is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    I have a method that is something like this...

    File file=new File(path);
    String path = f.getPath();

    boolean exists = file.exists();
    if (!exists) {
    create and save the the file.
    }else{
    the file exists so i just need to save it.
    }

    This is not working but i don't know very well how to do it right because of the file and path variables... :confused:

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

    Default

    Can you give us more information on what is specifically not working? Those methods work for me, and I can't tell based on what you've posted what you're doing wrong.

  5. #5
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    I don't understand your last line [“This is not working but i don't know very well how to do it right because of the file and path variables...”], but is any exception thrown, if not, what values does ‘exists’ get?

    I'm not sure, but I think ‘f.getPath()’ returns the relative path you fed the constructor;
    however, ‘f.getAbsolutePath()’ returns the absolute path and ‘f.getCanonicalPath()’ returns the absolute path a the file the path points to, so normally the file at your path, but if is a link in Unix-like OS:es it returns the absolute path of the file the link links to.

  6. #6
    Ballister is offline Member
    Join Date
    Dec 2010
    Posts
    3
    Rep Power
    0

    Default

    I have text on a textpane. When i hit the save button i want to know if the file already exists on the disk because in that case i want to update that file and if that file does not exists in the disc i want to create it with the text which is inside the textpane.

    What i post throws this exception -> Exception in thread "AWT-EventQueue-0" java.lang.NullPointerException

  7. #7
    Hibernate's Avatar
    Hibernate is offline Senior Member
    Join Date
    Dec 2010
    Location
    Stockholm, Sweden
    Posts
    222
    Blog Entries
    9
    Rep Power
    4

    Default

    That exception should not have anything to do with saving.
    Can you post the entire exception dump?

Similar Threads

  1. Japplet.getImage(): What if image file does not exist?
    By CoderMan in forum Java Applets
    Replies: 0
    Last Post: 02-11-2010, 11:31 PM
  2. HttpClientLibrary does not exist
    By gbose in forum Networking
    Replies: 1
    Last Post: 02-03-2010, 07:21 PM
  3. java.io.File - Files do not exist
    By TannerLD in forum New To Java
    Replies: 8
    Last Post: 09-10-2009, 12:03 AM
  4. package mj does not exist
    By PzK in forum New To Java
    Replies: 8
    Last Post: 12-03-2008, 10:30 PM
  5. Replies: 3
    Last Post: 11-03-2007, 01:58 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
  •