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?
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.
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?
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.
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
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
A better way to learn is to just try it and see for yourself, don't you think?
db