-2

I'm wondering how to make it so that my program creates a file if it does not already find it there?

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();
        }

And this is the error I am getting:

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)

All the extra stuff there is because my program is also a GUI program. And then I also have a method that's supposed to write my object to a file...

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

This throws the same exception. How can I avoid this? I'm not sure how to modify the FIS/OIS to create the file if they don't find it.. or I suppose it would be more efficient for the FOS/OOS to do it instead?

blue sky
  • 161
  • 1
  • 13
  • 2
    Err, don't try to open a non-existent file? In this case you appear to be missing a leading `/` in the filename. – user207421 Apr 09 '16 at 08:35
  • 1
    Possible duplicate of [check file exists java](http://stackoverflow.com/questions/1237235/check-file-exists-java) – Ducaz035 Apr 09 '16 at 08:36
  • @Ducaz035 Definitely not. That one is about appending to an existing file. – user207421 Apr 09 '16 at 08:38
  • 2
    You can't open an empty file with an ObjectInputStream, so creating it won't help. You should instead handle a missing file, and you should give it the correct path. – Peter Lawrey Apr 09 '16 at 08:48

1 Answers1

1
Users/...

You are missing a leading / in the filename. So you can't create it, so you can't read it.

user207421
  • 305,947
  • 44
  • 307
  • 483