我尝试从 src/resources 文件夹中打开一个 .txt 文件(agenda.txt),从中读取对象并将它们添加到 ArrayList,但出现此错误:“系统找不到指定的路径。”。这是我使用的代码:
public void open(File f) {
FileInputStream fis = null;
ObjectInputStream ois = null;
try {
fis = new FileInputStream(f);
ois = new ObjectInputStream(fis);
Object o;
try {
while ((o = ois.readObject()) != null) {
model.adauga((Abonat) o);
}
} catch (ClassNotFoundException ex) {
JOptionPane.showMessageDialog(
this,
ex.getMessage(),
"Clasa...!",
JOptionPane.ERROR_MESSAGE);
return;
}
} catch (IOException ex) {
JOptionPane.showMessageDialog(
this,
ex.getMessage(),
"Eroare deschidere fisier!",
JOptionPane.ERROR_MESSAGE);
return;
} finally {
try {
ois.close();
} catch (IOException ex) {
Logger.getLogger(CarteDeTelefonGUI.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
在类构造函数中:
private String path ="resources/agenda.txt";
File f=new File(path);
open(f);
代码有什么问题?