小编典典

Java中的FileNotFound异常

java

(我是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

}

阅读 280

收藏
2020-11-30

共1个答案

小编典典

查看消息:

发生了异常:java.io.FileNotFoundException:users / stefan / desktop / lol.xml

请注意,它是"users/stefan/[...]"-相对文件名,因此将相对于当前工作目录进行解析。您确定不是"/users/stefan/desktop/lol.xml"用斜杠表示绝对文件名吗?

2020-11-30