Java 中的 FileNotFound 异常

发布于 2024-12-07 00:30:29 字数 1622 浏览 0 评论 0原文

(我是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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

慈悲佛祖 2024-12-14 00:30:29

看一下留言:

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

请注意,它是 "users/stefan/[...]" - 它是一个相对文件名,因此将是相对于当前工作目录解析。您确定您的意思不是带有前导斜杠的 "/users/stefan/desktop/lol.xml" 来指示绝对文件名吗?

Look at the message:

An exception has occured : java.io.FileNotFoundException: users/stefan/desktop/lol.xml

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文