(我是Java新手)...我想在HashMap中存储一些类字段值,然后将其写入文件(路径作为参数传递),然后恢复HashMap并获取所需信息。在我的构造函数Carte中,我得到一个异常,找不到文件,无论如何它都在正确的位置,保存的数据在我的xml文件中。关于这一点的任何想法
发生了异常:java.io.FileNotFoundException:users / stefan / desktop / lol.xml(没有这样的文件或目录)
// Salveaza toate obiectele create intr-un fisier
public void salveazaObiecteleCreate(String caleSpreFisier) {
HashMap table = new HashMap();
table.put("Autorul", numelePrenumeleAutorului);
table.put("Denumirea cartii", denumireaCartii);
table.put ("Culoarea cartii",culoareaCartii);
table.put ("Genul cartii ",gen);
table.put ("Limba",limba);
table.put ("Numarul de copii",numarulDeCopii);
table.put ("Numarul de pagini",numarulDePagini);
table.put ("Pretul cartii",pretulCartii);
try {
File file = new File(caleSpreFisier);
FileOutputStream f = new FileOutputStream(file);
ObjectOutputStream s = new ObjectOutputStream(f);
s.writeObject(table);
s.close();
} catch(Exception e){
System.out.println("An exception has occured");
}
}
public Carte (String caleSpreFisier) {
HashMap table = new HashMap();
File file = new File(caleSpreFisier);
try {
FileInputStream f = new FileInputStream(file);
ObjectInputStream s = new ObjectInputStream(f);
table = (HashMap)s.readObject();
s.close();
} catch(Exception e){
System.out.println("An exception has occured : "+e);
}
for (Object key: table.keySet()) {
System.out.println(table.get(key));
}
}
// end of class
}
答案 0 :(得分:4)
查看消息:
发生了异常:java.io.FileNotFoundException:users / stefan / desktop / lol.xml
请注意它是"users/stefan/[...]"
- 它是相对文件名,因此将相对于当前工作目录进行解析。你确定你的意思是"/users/stefan/desktop/lol.xml"
并带有前导斜杠来表示绝对文件名吗?