Java 中的 FileNotFound 异常
(我是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
}
(i'm new in Java) ... I want to store some class fields values in a HashMap then write it to a file (the path is passed as an argument) and then restore HashMap and fetch needed info. In my constructor called Carte, i get an exception that file is not found , anyway it's in the right place and saved data is in my xml file. Any ideas on this point
An exception has occured : java.io.FileNotFoundException: users/stefan/desktop/lol.xml (No such file or directory)
// 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
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
看一下留言:
请注意,它是
"users/stefan/[...]"
- 它是一个相对文件名,因此将是相对于当前工作目录解析。您确定您的意思不是带有前导斜杠的"/users/stefan/desktop/lol.xml"
来指示绝对文件名吗?Look at the message:
Note that it's
"users/stefan/[...]"
- it's a relative filename, so will be resolved relative to the current working directory. Are you sure you didn't mean"/users/stefan/desktop/lol.xml"
with a leading slash to indicate an absolute filename?