FileInputStream找不到文件 [英] FileInputStream not finding the file

查看:1024
本文介绍了FileInputStream找不到文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在用Java编写一个程序,该程序将读取文件并将其中的每个单词放入数组,以便在将每个单词排序为默认数组后可以对它们进行拼写.除了不读取我的.txt文件外,我对如何执行此操作有个很好的主意.我使用编写的"anagram.java"程序在src中有一个名为"input.txt"的文件,但是当代码提示输入该文件时,在输入文件名"input.txt"时,我的代码会说该文件不存在,我明白了:

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:

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)

这是搞乱行的代码:

    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);
    }

显然,输入"input.txt"还不够,我不确定.我删除了

Apparently typing in "input.txt" is not enough information or something, I'm not sure. I removed the

    if(file.length() == 0)
    {
        System.out.println("File is empty");
        System.exit(1);
    }

要获取我在上面指出的错误,这就是我发现它甚至都无法使用anagram.java prgm识别src中的文件的方式.

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.

我的代码有什么问题?为什么不读取文件或说文件不存在?

What is wrong with my code? Why is it not reading the file or saying it isn't there?

推荐答案

我敢说文件位于src目录中-但我怀疑这不是程序的当前工作目录.要对此进行检查,请运行以下代码:

I dare say the file is in the src directory - but I suspect that's not the current working directory of the program. To check this, run this code:

System.out.println(new File(".").getAbsolutePath());

选项:

  • 指定绝对文件名
  • 指定一个相对文件名,该文件名应考虑您在其中运行的位置
  • 将文件捆绑为资源,并使用Class.getResourceAsStream或类似的文件
  • 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

请注意,这与BufferedReader无关-您正在从System.in读取文本,没有问题.

Note that this has nothing to do with BufferedReader - you're reading the text from System.in with no problems.

这篇关于FileInputStream找不到文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆