Ethan Z
Ethan Z

Reputation: 228

Unable to create new directory in documents or Program Files in Java

I apologize in advance if this is nooby, however I am new to dealing with files that are not in the same directory as the jar.

I'm unable to create new directory in documents or Program Files using the following:

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

And the trace:

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)

What I'm really trying to do is save data to a location other than the desktop, however it seems that I can't do this. Anyone know something I don't?

By request, the static method in 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;
    }

Upvotes: 0

Views: 442

Answers (1)

malkomich
malkomich

Reputation: 310

Have you tried the following? :

JSONObject jsonObject = JSONUtils.readFile(file);

I don't know the version of the library which contains JSONUtils class, but i think its method "readFile" must have the file as parameter instead the path of the file.

I hope it helps.

Upvotes: 1

Related Questions