【问题标题】:How to avoid FileNotFoundException? [closed]如何避免 FileNotFoundException? [关闭]
【发布时间】:2016-07-30 14:22:06
【问题描述】:

我想知道如何制作它,以便我的程序在找不到文件时创建一个文件?

public void load() throws Exception {
    try
      {
        File log = new File(FILE_NAME); //opens the file
        FileInputStream fileIn = new FileInputStream(log);
        ObjectInputStream in = new ObjectInputStream(fileIn); //de-serializes
        videosList = (ArrayList)in.readObject(); // de-serializes
        in.close(); //closes the file
        fileIn.close();
      } catch(Exception i)
      {
         i.printStackTrace();
        }

这是我得到的错误:

java.io.FileNotFoundException: Users/hanaezz/Desktop/output.ser (No such file or directory)
    at java.io.FileInputStream.open0(Native Method)
    at java.io.FileInputStream.open(FileInputStream.java:195)
    at java.io.FileInputStream.<init>(FileInputStream.java:138)
    at videostore.BinaryFile.load(BinaryFile.java:31)
    at videostore.VideoStore.<init>(VideoStore.java:33)
    at videostore.VideoStore$6.run(VideoStore.java:430)
    at java.awt.event.InvocationEvent.dispatch(InvocationEvent.java:311)
    at java.awt.EventQueue.dispatchEventImpl(EventQueue.java:756)
    at java.awt.EventQueue.access$500(EventQueue.java:97)
    at java.awt.EventQueue$3.run(EventQueue.java:709)
    at java.awt.EventQueue$3.run(EventQueue.java:703)
    at java.security.AccessController.doPrivileged(Native Method)
    at java.security.ProtectionDomain$JavaSecurityAccessImpl.doIntersectionPrivilege(ProtectionDomain.java:76)
    at java.awt.EventQueue.dispatchEvent(EventQueue.java:726)
    at java.awt.EventDispatchThread.pumpOneEventForFilters(EventDispatchThread.java:201)
    at java.awt.EventDispatchThread.pumpEventsForFilter(EventDispatchThread.java:116)
    at java.awt.EventDispatchThread.pumpEventsForHierarchy(EventDispatchThread.java:105)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:101)
    at java.awt.EventDispatchThread.pumpEvents(EventDispatchThread.java:93)
    at java.awt.EventDispatchThread.run(EventDispatchThread.java:82)

所有额外的东西都是因为我的程序也是一个 GUI 程序。然后我还有一个方法应该将我的对象写入文件...

public void writeToFile() throws Exception{
        FileOutputStream outputStream = new FileOutputStream(FILE_NAME);
        ObjectOutputStream oos = new ObjectOutputStream(outputStream);
        oos.writeObject(videosList);
        oos.close();
        outputStream.close();
        }

这会引发相同的异常。我怎样才能避免这种情况?如果他们找不到文件,我不确定如何修改 FIS/OIS 以创建文件。或者我认为 FOS/OOS 这样做会更有效?

【问题讨论】:

  • Err,不要尝试打开一个不存在的文件?在这种情况下,您似乎在文件名中缺少前导 /
  • check file exists java的可能重复
  • @Ducaz035 绝对不是。那是关于附加到现有文件。
  • 你不能用 ObjectInputStream 打开一个空文件,所以创建它也无济于事。相反,您应该处理丢失的文件,并且应该给它正确的路径。

标签: java arraylist file-io exception-handling


【解决方案1】:
Users/...

您在文件名中缺少前导 /。所以你不能创造它,所以你不能阅读它。

【讨论】:

  • 您能否用更多工作代码详细说明您的答案
  • @Suganthan 你在开玩笑吧?您不知道如何在文件名中添加前导 /
  • 但是是不是没有用户密码对..?
  • @Suganthan 这甚至不是一句话,更不用说一个问题了。
  • 你是对的吗?但这会对 OP 有帮助吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2010-09-22
  • 2010-12-14
  • 2016-03-31
  • 2019-07-02
  • 2018-12-21
  • 2014-09-11
  • 2021-07-30
相关资源
最近更新 更多