我想知道怎么做才能让我的程序创建一个文件,如果它还没有找到它?
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更有效率呢?
答案 0 :(得分:1)
Users/...
您错过了文件名中的前导/
。因此,您无法创建它,因此您无法阅读它。