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 5 of 5

Thread: what happens when you supply the same name of a nonexistent input file?

  1. #1
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Smile what happens when you supply the same name of a nonexistent input file?

    i have a LineNumberer program that i wanna know what happens when i supply the name of a nonexistent input file to this program? does it just throw a FileNotFound exception?


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

    Default Re: what happens when you supply the same name of a nonexistent input file?

    Not necessarily. Files can hold any file/directory path whether they exist or don't. You can check if a file exists using the exists() method.

    File file = new File("garbage.txt");
    if (file.exists())
    {
         System.out.println("You have garbage on your computer!");
    }
    else
    {
         System.out.println("There is no garbage on your computer!");
    }

    However, if you try to open up a non-existent file for reading/writing, you will get a FileNotFoundException.

  3. #3
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: what happens when you supply the same name of a nonexistent input file?

    Um, unless you are overwriting an existing file, it's normal to "open" a "nonexistent" file for writing by constructing a FileOutputStream etc. with the non-existent file as argument.

    Attempting to open a non-existent file for reading (is with an InputStream) will, as staed, throw an FNFE.

    db

  4. #4
    Junior Member
    Join Date
    Jan 2010
    Location
    norcross, ga
    Posts
    24
    Thanks
    7
    Thanked 0 Times in 0 Posts

    Default Re: what happens when you supply the same name of a nonexistent input file?

    What happens when you supply the same name for the input and output files to the LineNumberer program?


    just trying to learn about writing data to files

  5. #5
    Member Darryl.Burke's Avatar
    Join Date
    Mar 2010
    Location
    Madgaon, Goa, India
    Posts
    494
    Thanks
    8
    Thanked 48 Times in 46 Posts

    Default Re: what happens when you supply the same name of a nonexistent input file?

    A better way to learn is to just try it and see for yourself, don't you think?

    db

Similar Threads

  1. 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
  2. Replies: 8
    Last Post: January 6th, 2010, 09:59 AM
  3. Values of Input
    By chronoz13 in forum What's Wrong With My Code?
    Replies: 10
    Last Post: November 8th, 2009, 03:46 AM
  4. Input file parsing to insert into DB
    By IDForums in forum File I/O & Other I/O Streams
    Replies: 3
    Last Post: September 30th, 2009, 02:29 AM
  5. Object creation from a input file and storing in an Array list
    By LabX in forum File I/O & Other I/O Streams
    Replies: 4
    Last Post: May 14th, 2009, 03:52 AM