如果這是個不方便的話,我提前表示歉意,但是我對處理與jar不在同一目錄中的文件是新手。無法在Java文檔或程序文件中創建新目錄
我無法在創建文檔使用或程序文件的新目錄下:
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;
}
和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)
我真正想要做的是保存數據到桌面以外的位置,但似乎我無法做到這一點。任何人都知道我不喜歡的東西?
根據要求,在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;
}
嘗試使用mkdirs()而不是mkdir()。 – rsutormin
@rsutormin沒有變化。 –
如果它沒有幫助,那麼確保你已經爲你想要創建子文件夾的文件夾寫入權限。 – rsutormin