我有一个从文件读取和写入的程序。我什至试图在其中创建文件,main
但它仍然无法正常工作。
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 在控制台中打印的内容:
- Work in progress D:\Info\Java workspace\Laborator_4\Data.txt 找不到文件,错误:java.io.FileNotFoundException:Data.txt(系统找不到指定的文件)线程“main”java.lang中的异常。在 main.app.main(app.java:23) 的 repository.Repository.(Repository.java:49) 的 NullPointerException