2015-09-16 39 views
0

如果这是个不方便的话,我提前表示歉意,但是我对处理与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; 
    } 
+1

尝试使用mkdirs()而不是mkdir()。 – rsutormin

+0

@rsutormin没有变化。 –

+0

如果它没有帮助,那么确保你已经为你想要创建子文件夹的文件夹写入权限。 – rsutormin

回答

1

你试过以下? :

JSONObject jsonObject = JSONUtils.readFile(file); 

我不知道其中包含JSONUtils类库的版本,但我认为它的方法“READFILE”必须有文件作为参数,而不是文件的路径。

我希望它有帮助。

+0

我写了JsonUtils。一秒。另外,真正的问题是我无法创建目录,也无法读取文件的内容。 –

+0

我试过这段代码,在我的情况下,它的工作原理。但是,当目录被创建时,打印“mkdir()”的结果无论如何都是错误的。 – malkomich

+0

在JsonUtil方法中添加的文件也拒绝写入,因此它现在可以工作。 –

相关问题