简体   繁体   English

FileNotFoundException(不是目录)

[英]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:我有一个文件 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".如果我将目录打印出来,我会得到“/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):我不断收到 FileNotFoundException,即使文件在那里:这是错误消息(reader = new Scanner(file) 在第 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.当无法访问文件时也会抛出FileNotFoundException Make sure your file has the correct permissions to allow Java to access it.确保您的文件具有正确的权限以允许 Java 访问它。

Another thing I saw in the code is that the path starts at "/Users/" .我在代码中看到的另一件事是路径从"/Users/"开始。 Shouldn't it be "C:/Users/" ?不应该是"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.因为您要求它将savedSettings.txt解释为目录。

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 .注意第二行savedSettings.txt后面是/Users For that to make sense, the savedSettings.txt needs to resolve to a directory.为此, savedSettings.txt需要解析为一个目录。 Apparently, it doesn't.显然,它没有。 Hence the error message saying "its not a directory" 1 .因此错误消息说“它不是目录” 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".如果我将目录打印出来,我会得到“/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.目前尚不清楚您实际打印的是什么,但它不是您传递给Scanner构造函数的File表示的路径名。 The exception message does not lie.异常消息不会说谎。


1 - Granted, the exception message is a bit misleading. 1 - 当然,异常消息有点误导。 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.但不幸的是,操作系统返回的错误信息是非特定的,Java 尝试找出哪个名称不是目录会很复杂(而且成本很高)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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