无法在Java中的文档或程序文件中创建新目录

时间:2015-09-16 00:11:10

标签: java

我提前道歉,如果这是nooby,但我不熟悉处理与jar不在同一目录中的文件。

我无法使用以下内容在文档或程序文件中创建新目录:

private JSONObject readSaveFile() { // line 17
    File file;

    String programPath = new JFileChooser().getFileSystemView().getDefaultDirectory().toString();
    System.out.println(programPath);
    if (programPath != null) {
        System.out.println(new File(programPath + "/HWFetcherSave").mkdir());
        programPath += "/HWFetcherSave/data.json";
        file = new File(programPath);
        try {
            file.createNewFile();
            //TODO log error

        } catch (IOException e) {
            e.printStackTrace();
        }
        JSONObject jsonObject = JSONUtils.readFile(file.getAbsolutePath());

        if (jsonObject == null) jsonObject = new JSONObject();
        return jsonObject;
    }
    return null;
}

跟踪:

C:\Users\Ethan\Documents
false
java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1006)
    at HomeworkFetcher.readSaveFile(HomeworkFetcher.java:27)
    at HomeworkFetcher.main(HomeworkFetcher.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)
java.io.FileNotFoundException: C:\Users\Ethan\Documents\HWFetcherSave\data.json (The system cannot find the path specified)
    at java.io.FileInputStream.open(Native Method)
    at java.io.FileInputStream.<init>(FileInputStream.java:146)
    at java.io.FileInputStream.<init>(FileInputStream.java:101)
    at java.io.FileReader.<init>(FileReader.java:58)
The specified path could not be used to find a file.
    at JSONUtils.readFile(JSONUtils.java:58)
    at HomeworkFetcher.readSaveFile(HomeworkFetcher.java:32)
    at HomeworkFetcher.main(HomeworkFetcher.java:14)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:57)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
    at java.lang.reflect.Method.invoke(Method.java:606)
    at com.intellij.rt.execution.application.AppMain.main(AppMain.java:134)

我真正想要做的是将数据保存到桌面以外的位置,但似乎我不能这样做。有人知道我不知道的事吗?

根据请求,JsonUtils中的静态方法

public static JSONObject readFile(String path) {
        try {
            return readFile(new BufferedReader(new FileReader(path)));
        } catch (FileNotFoundException e) {
            System.out.println("The specified path could not be used to find a file.");
            e.printStackTrace();
        }
        return null;
    }

public static JSONObject readFile(BufferedReader reader) {
        try {
            return (JSONObject) new JSONParser().parse(reader);
        } catch (ParseException e) {
            System.out.println("Failed to parse file.");
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                reader.close();
            } catch (IOException e) {
            }
        }
        return null;
    }

1 个答案:

答案 0 :(得分:1)

您是否尝试过以下操作? :

JSONObject jsonObject = JSONUtils.readFile(file);

我不知道包含JSONUtils类的库的版本,但我认为它的方法&#34; readFile&#34;必须将文件作为参数而不是文件的路径。

我希望它有所帮助。