文件未使用createNewFile java方法创建

时间:2013-02-16 16:36:43

标签: java java-io

我有一个读取方法,如果它不存在,将尝试创建一个新文件,但是当我运行代码时,我得到一个(系统找不到指定的文件)错误。有人可以解释为什么它不创建文件?我还想知道如何在父目录中创建文件而不是当前将其保存在文件中的文件?

代码:

public Set<String> readWindowTitleSet() {
    try {
        File file = new File("windowData.out");

        String absolutePath = file.getAbsolutePath();
        String filePath = absolutePath.
            substring(0,absolutePath.lastIndexOf(File.separator));


        if (!file.exists()) {
            file.createNewFile();
        }

        FileInputStream fis = new FileInputStream(filePath +"\\..\\" + "windowData.out");
        // this is for testing in Eclipse.


        //FileInputStream fis = new FileInputStream(absolutePath);
        // This is for use in Jar file.

        ObjectInputStream ois = new ObjectInputStream(fis);
        anotherList = (HashSet<String>) ois.readObject();
        ois.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    return anotherList;

}

错误

java.io.FileNotFoundException: C:\Users\Joe\workspace\VocabTool\..\windowData.out (The     system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(Unknown Source)
at java.io.FileInputStream.<init>(Unknown Source)
at model.WindowTitleDataStore.readWindowTitleSet(WindowTitleDataStore.java:60)
at model.WindowTitleDataStore.<init>(WindowTitleDataStore.java:18)
at controller.InterruptionDecision.<clinit>(InterruptionDecision.java:23)
at view.MainGui.<init>(MainGui.java:29)
at view.MainGui.main(MainGui.java:38)
Exception in thread "main" java.lang.ExceptionInInitializerError
at view.MainGui.<init>(MainGui.java:29)
at view.MainGui.main(MainGui.java:38)
Caused by: java.lang.NullPointerException
at model.WindowTitleDataStore.<init>(WindowTitleDataStore.java:19)
at controller.InterruptionDecision.<clinit>(InterruptionDecision.java:23)
... 2 more

路径更改后出错

java.io.EOFException
at java.io.ObjectInputStream$PeekInputStream.readFully(Unknown Source)
at java.io.ObjectInputStream$BlockDataInputStream.readShort(Unknown Source)
at java.io.ObjectInputStream.readStreamHeader(Unknown Source)
at java.io.ObjectInputStream.<init>(Unknown Source)
at model.WindowTitleDataStore.readWindowTitleSet(WindowTitleDataStore.java:63)
at controller.InterruptionDecision.interruptNow(InterruptionDecision.java:165)
at controller.InterruptionDecision.updateWindowTitle(InterruptionDecision.java:74)
at controller.FocusWindowName.getFocusWindowTitle(FocusWindowName.java:27)
at controller.FocusWindowName.run(FocusWindowName.java:42)
at java.lang.Thread.run(Unknown Source)
Exception in thread "Thread-6" java.lang.NullPointerException
at model.WindowTitleDataStore.getWindowDataSize(WindowTitleDataStore.java:105)
at     controller.InterruptionDecision.getWindowDataStoreSize(InterruptionDecision.java:95)
at controller.InterruptionDecision.interruptNow(InterruptionDecision.java:168)
at controller.InterruptionDecision.updateWindowTitle(InterruptionDecision.java:74)
at controller.FocusWindowName.getFocusWindowTitle(FocusWindowName.java:27)
at controller.FocusWindowName.run(FocusWindowName.java:42)
at java.lang.Thread.run(Unknown Source)

1 个答案:

答案 0 :(得分:1)

从您的路径中删除..

应该是
FileInputStream fis = new FileInputStream(filePath +"\\windowData.out");

而不是
FileInputStream fis = new FileInputStream(filePath +"\\..\\" + "windowData.out");

因为filePath +"\\..\\" + "windowData.out"将等于C:\Users\Joe\workspace\VocabTool\..\windowData.out,而filePath +"\\windowData.out"不是您文件的有效位置。

虽然C:\Users\Joe\workspace\VocabTool\windowData.out将等于{{1}},但我猜你的文件的有效位置(猜猜你在指定目录中有一个文件windowData.out)