java FileInputStream 找不到文件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15368041/
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
FileInputStream not finding the file
提问by Ryan Tibbetts
I am making a program in java that will read a file and put each word from it into an array so I can make an anagram of each word after sorting them to a default array. I have a good idea of how to do this, except my .txt file is not being read. I have a file called "input.txt" in the src with the "anagram.java" program I am writing, but when the code promts for the file entry, upon entering the file name "input.txt" my code says the file does not exist and I get this:
我正在用java编写一个程序,它将读取一个文件并将其中的每个单词放入一个数组中,这样我就可以在将它们排序为默认数组后制作每个单词的字谜。我很清楚如何执行此操作,但我的 .txt 文件没有被读取。我在 src 中有一个名为“input.txt”的文件,其中包含我正在编写的“anagram.java”程序,但是当代码提示文件条目时,在输入文件名“input.txt”时,我的代码显示该文件不存在,我得到了这个:
Enter file name:
input.txt
Exception in thread "main" java.io.FileNotFoundException: input.txt (No such file or directory)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:120)
at java.io.FileInputStream.<init>(FileInputStream.java:79)
at java.io.FileReader.<init>(FileReader.java:41)
at anagram.main(anagram.java:23)
Java Result: 1
BUILD SUCCESSFUL (total time: 6 seconds)
Here is the code at the line where is messing up:
这是混乱所在行的代码:
public static void main(String[] args) throws Exception {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("Enter file name: ");
String fileName = br.readLine();
File file = new File(fileName);
if(file.length() == 0)
{
System.out.println("File is empty");
System.exit(1);
}
Apparently typing in "input.txt" is not enough information or something, I'm not sure. I removed the
显然,输入“input.txt”是不够的信息或其他东西,我不确定。我删除了
if(file.length() == 0)
{
System.out.println("File is empty");
System.exit(1);
}
To get the error I stated above, which is how I figured out it wasn't even recognizing the file in the src with the anagram.java prgm.
为了得到我上面提到的错误,这就是我发现它甚至无法使用 anagram.java prgm 识别 src 中的文件的方式。
What is wrong with my code? Why is it not reading the file or saying it isn't there?
我的代码有什么问题?为什么它不读取文件或说它不存在?
回答by Jon Skeet
I dare say the file is in the src
directory - but I suspect that's notthe current working directory of the program. To check this, run this code:
我敢说该文件在src
目录中 - 但我怀疑这不是程序的当前工作目录。要检查这一点,请运行以下代码:
System.out.println(new File(".").getAbsolutePath());
Options:
选项:
- Specify an absolute filename
- Specify a relative filename which takes into account where you're running this
- Bundle the file as a resource and use
Class.getResourceAsStream
or similar
- 指定绝对文件名
- 指定一个相对文件名,它考虑到您运行它的位置
- 将文件捆绑为资源并使用
Class.getResourceAsStream
或类似
Note that this has nothing to do with BufferedReader
- you're reading the text from System.in
with no problems.
请注意,这与BufferedReader
您System.in
没有任何问题无关- 您正在阅读文本。
回答by scaryman
It's looking in a path different than your source directory. Try specifying a full path like c:\input.txt
(but don't forget to move your file there!) to see what I mean.
它在与源目录不同的路径中查找。尝试指定一个完整的路径c:\input.txt
(但不要忘记将文件移到那里!)以了解我的意思。