The FileReader cannot find my text file

。_饼干妹妹 提交于 2019-12-03 21:38:38

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.


Sample code:

Reading a file abc.txt from resources folder

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

Here is the project structure


Try below code to find out where it is pointing to 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.txt file in the project RankFavorites directly as shown in below snapshot and It will definitely solve your problem.

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"

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

File sdcard = Environment.getExternalStorageDirectory();

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

BufferedReader br = new BufferedReader(new FileReader(file));
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!