Java FileReader 找不到我的文本文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/22585621/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-13 16:39:26  来源:igfitidea点击:

The FileReader cannot find my text file

javafilereaderjava-io

提问by user2843235

I have a simple text file and for some reason, it cannot be found. I don't see anything wrong with the code because I got it from a site, and I am starting to think that I didn't place the text file in the right place. Any suggestion please?

我有一个简单的文本文件,由于某种原因,找不到它。我没有发现代码有任何问题,因为我是从一个站点获取的,而且我开始认为我没有将文本文件放在正确的位置。请问有什么建议吗?

Code:

代码:

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.nio.file.Path;
import java.nio.file.Paths;

public class MainFavorites {
    public static void main(String[] args) throws IOException {
        /**
        * finds pathway to the file
        */
        //      File file = new File("icecreamTopping.txt");
        //      System.out.println(file.getCanonicalPath());

        BufferedReader reader = null;
        ArrayList <String> myFileLines = new ArrayList <String>();
        try {
            String sCurrentLine;
            reader = new BufferedReader(new FileReader("icecreamTopping.txt"));
            while ((sCurrentLine = reader.readLine()) != null) {
                //System.out.println(sCurrentLine);
                myFileLines.add(sCurrentLine);
            }
        } catch (IOException e) {
            e.printStackTrace();
            System.out.print(e.getMessage());
        } finally {
            try {
                if (reader != null)reader.close();
            } catch (IOException ex) {
                System.out.println(ex.getMessage());
                ex.printStackTrace();
            }
        }
        int numElements = myFileLines.size();
        System.out.println ("there are n lines in the file:" + numElements);

        for (int counter = numElements-1; counter >= 0; counter--) {
            String mylineout = myFileLines.get(counter);
            System.out.println (mylineout);
        }
    }
}

File content:

文件内容:

1- Blueberry 
2- Banana Buzz
3- Cookie Batter

My stack trace is this:

我的堆栈跟踪是这样的:

java.io.FileNotFoundException: C:\Users\homar_000\workspace\RankFavorites\icecreamTopping.txt (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method) at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileReader.<init>(Unknown Source)
at MainFavorites.main(MainFavorites.java:28)

采纳答案by user2843235

Found out what was the problem. It was unnecessary for me to put the file extension so I removed the .txt because when I kept it, it read it as "icecreamTopping.txt.txt"

找出问题所在。我没有必要放置文件扩展名,所以我删除了 .txt,因为当我保留它时,它读作“icecreamTopping.txt.txt”

回答by Braj

Replace below line

替换下面的行

reader = new BufferedReader(new FileReader("icecreamTopping.txt"));

with

reader = new BufferedReader(new FileReader("resources/icecreamTopping.txt"));

and put the file under resources folder that resides parallel to src folder.

并将文件放在与 src 文件夹平行的资源文件夹下。



Sample code:

示例代码:

Reading a file abc.txtfrom resources folder

abc.txt从资源文件夹中读取文件

reader = new BufferedReader(new FileReader("resources/abc.txt"));

Here is the project structure

这是项目结构

enter image description here

在此处输入图片说明



Try below code to find out where it is pointing to file icecreamTopping.txt.

尝试下面的代码以找出它指向 file 的位置icecreamTopping.txt

 File f=new File("icecreamTopping.txt");
 System.out.println(f.getAbsolutePath());

After getting the absolute path, just place the file there.

获得绝对路径后,只需将文件放在那里。



--EDIT--

- 编辑 -

As per your last comments, put icecreamTopping.txtfile in the project RankFavoritesdirectly as shown in below snapshot and It will definitely solve your problem.

根据您最后的评论,将icecreamTopping.txt文件RankFavorites直接放入项目中,如下面的快照所示,它肯定会解决您的问题。

enter image description here

在此处输入图片说明

回答by Govtart

Try to use File class to detect your file in storage:

尝试使用 File 类来检测存储中的文件:

File sdcard = Environment.getExternalStorageDirectory();

//Get the text file
File file = new File(sdcard,"file.txt");

BufferedReader br = new BufferedReader(new FileReader(file));