1

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
8
  • You could move the file to C: drive and test it. "yeh_vidhi_mangal.txt" what does mean :) ?
    – nayakam
    Commented Dec 22, 2011 at 5:42
  • Try getting rid of the double slashes in your path except for the C:\\ Commented Dec 22, 2011 at 5:43
  • @luxun NO! that will give you an illegal escape character. :)
    – COD3BOY
    Commented Dec 22, 2011 at 5:47
  • Are all the directory name cases correct?
    – Steven
    Commented Dec 22, 2011 at 5:49
  • 1. C:/Users/Siddharth/Documents/aarti/yeh_vidhi_mangal.txt is giving me same error 2. I tried moving the file to c:, that also does not fix it 3. I am using win7 and eclipse 4. how can i compile and execute from command line to check if its an eclipse problem?
    – morpheus
    Commented Dec 22, 2011 at 5:51

2 Answers 2

8

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.

2
  • file.canRead() is returning true.
    – morpheus
    Commented Dec 22, 2011 at 6:09
  • @morpheus try luck with canExecute()
    – COD3BOY
    Commented Dec 22, 2011 at 6:20
0

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.
2
  • NO! this will lead to illegal escape character error :) If you want to write a \ in a String literal you have to write \\
    – COD3BOY
    Commented Dec 22, 2011 at 6:47
  • Also, it is not necessary. Forward slashes will work for a pathname in Java ... even on Windows; see softwareengineering.stackexchange.com/questions/316700/…
    – Stephen C
    Commented Aug 3, 2019 at 4:15

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.