我尝试从src/resources文件夹打开一个.txt文件(agenda.txt),从它读取对象并将它们添加到ArrayList中,但是我得到了这个错误:“系统找不到指定的路径”。这是我的代码使用方法:Java打开文件错误:找不到指定的路径
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);
什么是错误的代码?
尝试资源// agenda.txt – user1889970