java IntelliJ“FileNotFoundException”,文件存在
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33271744/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
IntelliJ "FileNotFoundException", File Exists
提问by Sugarcoder
My objective is to read a text file on IntelliJ. However, when I ran my codes, I get a "FileNotFoundException" message. My file exists. I triple-checked to make sure that the path is correct. I've scoured Stack Overflow looking for an answer, read every question I've come across, but no one offered a concrete solution to the issue.
我的目标是在 IntelliJ 上读取文本文件。但是,当我运行我的代码时,我收到一条“FileNotFoundException”消息。我的文件存在。我三重检查以确保路径正确。我已经搜索 Stack Overflow 寻找答案,阅读我遇到的每个问题,但没有人为这个问题提供具体的解决方案。
This is my code:
这是我的代码:
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class LetterGrader {
public static void main(String[] args) {
String curDir = new File(".").getAbsolutePath();
System.out.println("Current sys dir: " + curDir);
try {
File inputData = new File("input.txt");
BufferedReader br = new BufferedReader(new FileReader(inputData));
} catch (IOException e) {
System.out.println("File not found");
e.printStackTrace();
}
}
}
This is the error message that showed up.
这是显示的错误消息。
File not found
java.io.FileNotFoundException: input.txt (The system cannot find the file specified)
at java.io.FileInputStream.open0(Native Method)
at java.io.FileInputStream.open(FileInputStream.java:195)
at java.io.FileInputStream.<init>(FileInputStream.java:138)
at java.io.FileReader.<init>(FileReader.java:72)
at LetterGrader.main(LetterGrader.java:23)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:497)
at com.intellij.rt.execution.application.AppMain.main(AppMain.java:140)
Process finished with exit code 0
Any help would be greatly appreciated! When I find a solution, I will respond back, too.
任何帮助将不胜感激!当我找到解决方案时,我也会回复。
[UPDATE]
[更新]
I solved the issue by moving my "input.txt" file out or src folder and into the main project's folder.
我通过将“input.txt”文件移出或 src 文件夹并移入主项目的文件夹解决了该问题。
I also used Scanner instead of BufferedReader.
我还使用了 Scanner 而不是 BufferedReader。
My final code that worked:
我的最终代码有效:
try {
Scanner diskScanner = new Scanner(new File("input.txt"));
while (diskScanner.hasNextLine()) {
System.out.println(diskScanner.nextLine());
}
diskScanner.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
回答by Devansh Maurya
The issue is because of the differences in the working directory, i.e., the directory where your source code resides and the current working directory of IntelliJ
is different. Generally, the working directory is the main directory of your project.
So, either you place your file in that directory, or use Edit Configurations...
option. After selecting Edit Configurations...
, check out the option Working directory
and change it accordingly.
问题是因为工作目录不同,即你的源代码所在的目录和当前工作目录IntelliJ
不同。通常,工作目录是项目的主目录。因此,要么将文件放在该目录中,要么使用Edit Configurations...
选项。选择后Edit Configurations...
,检查该选项Working directory
并相应地更改它。
回答by antonpp
You can try to print absolute path of the file first
您可以尝试先打印文件的绝对路径
System.out.println(new File("input.txt").getAbsolutePath());
Then, since you are using IntelliJ, you can open terminal right in IDE and try to open this file from there. For example:
然后,由于您使用的是 IntelliJ,您可以直接在 IDE 中打开终端并尝试从那里打开此文件。例如:
cat /Users/antonpp/Documents/Projects/testapp/input.txt
So, you will be totally sure that file exists and it is in the right place.
因此,您将完全确定该文件存在并且位于正确的位置。
回答by Zero
Oh, I also have some problems about that. So try Edit Configurations
. You can find it at Run -> Edit Configurations.. Then edit Working directory
to where your file locates. By the way, you can edit it to base directory and add path in your code.
哦,我也有一些问题。所以试试吧Edit Configurations
。您可以在“运行”->“编辑配置”中找到它。然后编辑Working directory
到您的文件所在的位置。顺便说一句,您可以将其编辑到基本目录并在代码中添加路径。