Java 文件未找到不合理的异常(对我而言)
我有一个从文件读写的程序。我甚至尝试在main中创建该文件,但仍然不起作用Java 文件未找到不合理的异常(对我而言),java,filenotfoundexception,Java,Filenotfoundexception,我有一个从文件读写的程序。我甚至尝试在main中创建该文件,但仍然不起作用 public static void main(String[] args) throws NumberFormatException, IOException, FileNotFoundException { System.o
public static void main(String[] args) throws NumberFormatException,
IOException,
FileNotFoundException {
System.out.println("Work in progress");
File f = new File("Data.txt");
System.out.println(f.getAbsolutePath());
// Yes, it's there.
UI ui = new UI();
GenericDictionary<Integer> gd = new GenericDictionary<Integer>();
Repository repo = new Repository("Data.txt", gd);
// Should work, right ?
现在,我的存储库:
public Repository (String fileName, GenericDictionary gd)
throws IOException,
NumberFormatException,
FileNotFoundException {
this.fileName = fileName;
this.gd = gd;
FileReader input = null;
BufferedReader inputBuffer = null;
try {
input = new FileReader(this.fileName);
inputBuffer = new BufferedReader (input);
String line;
while ((line = inputBuffer.readLine()) != null) {
String[] inputData = line.split(",");
Node<Integer> newNode = new Node<Integer> (
Integer.parseInt(inputData[0]),
Integer.parseInt(inputData[1]));
this.gd.add(newNode);
}
}catch (NumberFormatException nfe){
System.out.println(
"Repository could not load data due to NumberFormatException: "
+ nfe);
}catch (FileNotFoundException fnfe) {
System.out.println("File not found, error: " + fnfe);
}finally {
inputBuffer.close();
input.close();
}
}
现在,即使我创建了我的文件,它也不想使用它。最初它在我的存储库的构造函数中,我将它移动到主文件中,但仍然没有成功
这是Eclipse在控制台中打印的内容:
正在进行的工作
D:\Info\Java workspace\Laborator\u 4\Data.txt
未找到文件,错误:java.io.FileNotFoundException:Data.txt系统找不到指定的文件
线程主java.lang.NullPointerException中出现异常
位于repository.repository.repository.java:49
在main.app.mainapp.java:23
这与您认为的不同:
File f = new File("Data.txt");
System.out.println(f.getAbsolutePath());
// Yes, it's there.
这不会在磁盘上创建文件。它只是创建一个表示路径名的文件对象。如果您使用:
System.out.println(f.exists());
这将告诉你它是否真的存在
因此,除非D:\Info\Java workspace\Laborator\u 4\Data.txt真的存在,否则获得异常是完全合理的。创建文件并重试
此外,您在finally块中得到一个NullPointerException,因为您假设inputBuffer和input都是非null的:不要这样假设。关闭前检查。文件是一个抽象路径。执行此命令:
File f = new File("Data.txt");
绝对不会在磁盘上执行任何操作。这也不是
System.out.println(f.getAbsolutePath());
对文件是否存在的任何测试
这样做:
if(file.exists()) {
// yes it's there
}
正如其他人所说,您不创建文件,请从这里尝试触摸法:我希望这能起作用: 更换这条线
Repository repo = new Repository("Data.txt", gd);
与:
Repository repo = new Repository(f, gd);
在你的
public Repository (String fileName, GenericDictionary gd)
throws IOException,
NumberFormatException,
FileNotFoundException
用这个
public Repository (File f, GenericDictionary gd)
throws IOException,
NumberFormatException,
FileNotFoundException
在try{}
而不是
input = new FileReader(this.fileName);
这样做
input = new FileReader(f.getAbsolutePath());
您不会在代码中创建文件。新文件不会在磁盘上创建真实文件。您考虑到了吗?请尝试使用绝对路径名,以避免潜在地查找错误的位置。另外,你可以调用file.createNewFile来有效地创建一个空白条目,我想这就是你想要的。哦,现在我觉得很傻。我只是手动检查,它确实不在那里,但我确实使用了绝对路径来查看它应该在哪里。手动添加,现在可以使用了,泰。顺便问一下,你如何创建一个文件呢?如何最好地检查它们是否为空?最后简单地把一个if放进去?如果它们是空的/不是空的,如果在那一点上对我来说不是同一件事,我该如何反应?@Kalec:是的,只要检查一下if输入就可以了!=null{input.close;},同样适用于inputBuffer。如果该值为null,则无需关闭……我认为这里不需要这样做。File.createNewFile也有同样的功能。你说得对。如果我对文件做任何事情,我总是使用ApacheFileUtils,因为它工作得更好,并且有很多很好的特性。但是,如果把罐子放在里面,只使用触控,那肯定是一种过度的杀伤力。