Reputation: 20310
I am trying to read a file located at "C:\Users\Siddharth\Documents\aarti\yeh_vidhi_mangal.txt". Following code indicates that file exists
String filename = "C:\\Users\\Siddharth\\Documents\\aarti\\yeh_vidhi_mangal.txt";
File file = new File(filename);
System.out.println(file.exists());
but when I try to open the file using
FileInputStream in = new FileInputStream(file);
a FileNotFoundException
is thrown. Is this because access is denied? I checked file permissions and they are fine.
I have also tried working without eclipse. from the command line:
C:\Users\Siddharth\workspace\file_io_test\src>javac Foo.java Foo.java:16: error: unreported exception FileNotFoundException; must be caught o r declared to be thrown FileInputStream in = new FileInputStream(file); ^ 1 error
Upvotes: 1
Views: 14609
Reputation: 115
Try using single slash rather than double slash like
C:/User/Documents/your Filename.
and use Backward Slash Because your File is going to inherit in the following Directories so write the path like this for ex:
C:\User\Documents\Your Filename.
Upvotes: 0
Reputation: 24167
According to the documentation for FileInputStream
, "If the named file does not exist, is a directory rather than a regular file, or for some other reason cannot be opened for reading then a FileNotFoundException is thrown." (emphasis mine) The file may be locked or in use by another application.
What does file.canRead()
return?
Now that you've updated your question with more data, I can see that you are misinterpreting the error message. The error is that you are calling a method which throws a certain type of exception and you are not properly reporting or handling the exception. You can either add a try
/ catch
for FileNotFoundException
or add a throws
to your method declaration which states that FileNotFoundException
can be thrown.
Upvotes: 8