简体   繁体   中英

FileNotFoundException (Not a directory)

I've been trying to read a text file for more than a day now, but no matter what I do, it always goes wrong.

I'll simplify my code:

I have a File object:

File file = new File(System.getProperty(user.dir) + "/src/com/ksa/voetje/savedFiles/savedSettings.txt");

If I print the directory out, I get "/Users/jonaseveraert/IdeaProjects/Voetje KSA/src/com/ksa/voetje/savedFiles/savedSettings.txt".

I also have a Scanner, and this is where I keep getting errors:

Scanner reader = null;
try {
   reader = new Scanner(file);
} catch (FileNotFoundException e) {
   e.printStackTrace();
} 

I keep getting the FileNotFoundException, even though the file is there: Here is the error message (reader = new Scanner(file) is at line 42):

java.io.FileNotFoundException: /Users/jonaseveraert/IdeaProjects/Voetje KSA/src/com/ksa/voetje/savedFiles/savedSettings.txt/Users/jonaseveraert/IdeaProjects/Voetje KSA/src/com/ksa/voetje/savedFiles/savedSettings.txt (Not a directory)
    at java.base/java.io.FileInputStream.open0(Native Method)
    at java.base/java.io.FileInputStream.open(FileInputStream.java:211)
    at java.base/java.io.FileInputStream.<init>(FileInputStream.java:153)
    at java.base/java.util.Scanner.<init>(Scanner.java:639)
    at com.ksa.voetje.savedFiles.FileReader.readLines(FileReader.java:42)
    at com.ksa.voetje.instellingen.previousSettings.SettingsReader.getPreviousSettings(SettingsReader.java:22)
    at com.ksa.voetje.instellingen.previousSettings.SettingsReader.<init>(SettingsReader.java:14)
    at com.ksa.voetje.instellingen.previousSettings.PreviousSettings.<init>(PreviousSettings.java:11)
    at com.ksa.voetje.display.DisplaySystem.<init>(DisplaySystem.java:94)
    at com.ksa.voetje.Main.start(Main.java:16)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:846)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:455)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:428)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:391)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:427)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)

The error tells me the path I gave to file is not a directory, I don't understand why it gives me this error.

Can someone please help me?

FileNotFoundException is also thrown when the file cannot be accessed. Make sure your file has the correct permissions to allow Java to access it.

Another thing I saw in the code is that the path starts at "/Users/" . Shouldn't it be "C:/Users/" ?

The error tells me the path I gave to file is not a directory, I don't understand why it gives me this error.

Because you asked it to interpret savedSettings.txt as a directory.

Oh yes you did!!

According to the exception message, this is the pathname for the file you were using:

 /Users/jonaseveraert/IdeaProjects/Voetje KSA/src/com/ksa/
 voetje/savedFiles/savedSettings.txt/Users/jonaseveraert/
 IdeaProjects/Voetje KSA/src/com/ksa/voetje/savedFiles/savedSettings.txt

(I have added some line breaks added for readability.)

Notice on the 2nd line savedSettings.txt is followed by /Users . For that to make sense, the savedSettings.txt needs to resolve to a directory. Apparently, it doesn't. Hence the error message saying "its not a directory" 1 .

The problem is in the way you are assembling the pathname... or what you are assembling it from.

You say:

If I print the directory out, I get "/Users/jonaseveraert/IdeaProjects/Voetje KSA/src/com/ksa/voetje/savedFiles/savedSettings.txt".

It is not clear what you are actually printing, but it is NOT the pathname denoted by the File that you are passing to the Scanner constructor. The exception message does not lie.


1 - Granted, the exception message is a bit misleading. It would be more helpful if it said that something in that path doesn't resolve to a directory. But unfortunately, the error information returned by the OS is non-specific, and it would be complicated (and expensive) for Java to try to work out which name isn't a directory.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM