3

I'm trying to use an absolute path to read an file from file system but failed because of the "FileNotFoundException", which I don't know why

File file=new File("E:\\Directory\\File.txt");
byte[] buff=new byte[8];
FileInputStream fileIn=new FileInputStream(file.getAbsolutePath());
int n=fileIn.read(buff);
System.out.println(n);
Gary
  • 13,303
  • 18
  • 49
  • 71
  • To confirm the file exists, copy the full path, remove the extra \ and paste it to windows explorer. – Jules Nov 05 '16 at 11:56

3 Answers3

0

Here are some of the things that might conceivably cause this problem:

  1. The file doesn't exist. (You say this is not the problem ...)
  2. The directory does exist but your application doesn't have permission to read it.
  3. The directory and file exist but you've got a problem with the true pathname or the pathname you are using:

    • It might have invisible / non-printing characters in it.
    • It might have trailing space characters, or different numbers of embedded spaces
    • It might be a homoglyph problem
  4. The actual error is happening somewhere else in your code.

  5. Somehow the code you are actually running doesn't match your source code; e.g. you have a methodological problem with your edit / compile / deploy / run procedure.

Can I also suggest that you try it like this:

File file = new File("E:/Directory/File.txt");
FileInputStream fileIn = new FileInputStream(file);

Java pathname handling should translate a "/" to the appropriate platform specific file separator. And file already denotes an absolute path, so calling file.getAbsolutePath() should not be necessary.

Stephen C
  • 698,415
  • 94
  • 811
  • 1,216
  • wouldn't `3.` be wrong, seeing as his pathname contains non of those? – ItamarG3 Nov 05 '16 at 11:54
  • The pathname in the code he showed us has none of those, but 1) this may not be the real code, and 2) we only have his word for it that the actual pathname is what he thinks it is. He would be advised to (really) check these things. And even **re-check** them. – Stephen C Nov 05 '16 at 11:58
  • I agree, but I try to make do with what we have, without assuming more. – ItamarG3 Nov 05 '16 at 11:59
  • @ItamarGreen - I think it has gotten past that. This scenario is implausible if the situation is truly as he is describing. – Stephen C Nov 05 '16 at 12:01
  • yes, I'm trying to figure out what's wrong. files in E don't normally need access permission, (as in, administrator access) – ItamarG3 Nov 05 '16 at 12:03
  • @ItamarGreen - Not normally. However, we are looking for >>possible<< explanations here, not >>likely<< ones. – Stephen C Nov 05 '16 at 12:06
  • quite right, I'm just saying that this is an odd question – ItamarG3 Nov 05 '16 at 12:07
0

Thanks for helping.I've already know why.Because it may throw Exceptions,so I should add exception to method signature or surround with try/catch. Actually when you use Intelli IDEA,"Alt+Enter"can help to figure out what's wrong with the your codes.....

-1

Well, the problem is exactly as the error states:

The file doesn't exist. i.e. there is no file in that path. The name you gave the constructor might be wrong. You can check if a file exist via:

File file=new File("E:\\Directory\\File.txt");
if(file.exists()){

    //do things here
}

You can also check if the file exists from windows explorer (assuming you run windows on your pc). If you can't find the file, then that explains the problem. As I stated, the error is thrown because there is no file with that path.

ItamarG3
  • 4,092
  • 6
  • 31
  • 44