使用Eclipse编辑器的FileNotFoundException异常。

问题描述 投票:0回答:1

我正在用Eclipse IDE编写一个Java类。我尝试用这个来读取一个文件。

FileReader file = new FileReader("dictionary.txt");

txt和这行代码在同一个文件的src目录下。文件名是正确的,但它抛出了java.io.FileNotFoundException.我如何解决这个问题?谢谢

java eclipse filereader filenotfoundexception
1个回答
0
投票

试着使用C:/\文件位置 -- 那它能工作吗?


0
投票

你需要提到你正在尝试从相对路径读取。要说的是。

FileReader file = new FileReader("src/dictionary.txt");

FileReader file = new FileReader("./src/dictionary.txt");

如果你的项目结构是这样的,它将会工作。

enter image description here


0
投票

请尝试给出完整的路径。我在Windows中试了一下。

 FileReader file = new FileReader("F:\\Java\\VD_program\\java_prog\\TestProj\\src\\main\\java\\com\\vish\\test\\dictionary.txt");

对于Linux,它将是''而不是'\'。

© www.soinside.com 2019 - 2024. All rights reserved.