Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: File exists or not

  1. #1
    Junior Member
    Join Date
    Dec 2010
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default File exists or not

    Hi

    So, 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 I want to create it with the text which is inside the textpane.

    When the the first hit on the save button will be this :
    if(fileChooser.showSaveDialog(this) != JFileChooser.APPROVE_OPTION)
                return;
     
            File f = fileChooser.getSelectedFile();
     
            if(f == null)
                return;
     
            try{
                FileOutputStream fout = new FileOutputStream(f);
                rtf.write(fout, textArea.getDocument(), 0, textArea.getDocument().getLength());
            }
            catch(IOException e) {}
            catch(BadLocationException e) {}
            catch(NullPointerException e) {}
            frame.setTitle(fileChooser.getSelectedFile().getName());
        }

    Ok, now I know that the file exists on the disc because with the code that i posted the final user saved it.

    The second hit in the same save button will just do the "update" of the file (modifications, etc).

    How can I do it ?

    Any tips please ?
    Last edited by helloworld922; December 26th, 2010 at 09:53 PM.


  2. #2
    Super Moderator helloworld922's Avatar
    Join Date
    Jun 2009
    Posts
    2,895
    Thanks
    23
    Thanked 619 Times in 561 Posts
    Blog Entries
    18

    Default Re: File exists or not

    The easiest way for the computer to save a file is to simply over-write it (either that or append to it, though this could lead to rather large files).

    You can check if a file exists before-hand using file.exists().

    // creates the file specified by f is it doesn't exist
    if(!f.exists())
    {
        f.createNewFile();
    }

    However, I this is unnecessary because FileOutputStream automatically creates the file if it doesn't exist before-hand.

  3. #3
    Junior Member
    Join Date
    Jan 2011
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: File exists or not

    java write files

    use two way , you can see: http://java-er.com/tutorial/java-writing-files/

Similar Threads

  1. Replies: 10
    Last Post: January 12th, 2011, 05:48 AM
  2. insert(embed) a file object (.txt file) in MS excel sheet using java.
    By jyoti.dce in forum What's Wrong With My Code?
    Replies: 1
    Last Post: August 12th, 2010, 08:16 AM
  3. How to change File Type of a File Using java
    By akash169 in forum File I/O & Other I/O Streams
    Replies: 8
    Last Post: March 31st, 2010, 02:58 AM
  4. Inputing file (.txt) and finding the highest number in the file
    By alf in forum What's Wrong With My Code?
    Replies: 3
    Last Post: March 15th, 2010, 09:11 AM
  5. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM