如何在Java中的项目文件夹中创建文件?

abs*_*ero 1 java

在JsonOperation类中:

public void writeJson(String path, JSONObject passedJsonObj){
    File file = new File(path);
    try{
        if (!file.exists()){
            file.createNewFile();
        }
        FileWriter writer = new FileWriter(file);
        writer.write(passedJsonObj.toJSONString());
        writer.flush();
        writer.close();
    }catch(IOException e){
        e.printStackTrace();
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的主叫班上:

    LocalDate todayDate = LocalDate.now();
    DateTimeFormatter formatter = DateTimeFormatter.ofPattern("dd/MM/yyyy");
    String dateString = todayDate.format(formatter).toString();

    JsonOperation jsonOp = new JsonOperation();
    jsonOp.writeJson("srcsample/SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );
Run Code Online (Sandbox Code Playgroud)

运行此程序时,出现以下错误:

java.io.IOException: The system cannot find the path specified
    at java.io.WinNTFileSystem.createFileExclusively(Native Method)
    at java.io.File.createNewFile(File.java:1012)
    at sample.JsonOperation.writeJson(JsonOperation.java:50)
    at sample.Main.saveData(Main.java:58)
    at sample.Main.start(Main.java:29)
    at com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$159(LauncherImpl.java:863)
    at com.sun.javafx.application.LauncherImpl$$Lambda$52/384953125.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$runAndWait$172(PlatformImpl.java:326)
    at com.sun.javafx.application.PlatformImpl$$Lambda$48/113087735.run(Unknown Source)
    at com.sun.javafx.application.PlatformImpl.lambda$null$170(PlatformImpl.java:295)
    at com.sun.javafx.application.PlatformImpl$$Lambda$50/949297714.run(Unknown Source)
    at java.security.AccessController.doPrivileged(Native Method)
    at com.sun.javafx.application.PlatformImpl.lambda$runLater$171(PlatformImpl.java:294)
    at com.sun.javafx.application.PlatformImpl$$Lambda$49/59984698.run(Unknown Source)
    at com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:95)
    at com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at com.sun.glass.ui.win.WinApplication.lambda$null$145(WinApplication.java:101)
    at com.sun.glass.ui.win.WinApplication$$Lambda$38/665838427.run(Unknown Source)
    at java.lang.Thread.run(Thread.java:745)
Run Code Online (Sandbox Code Playgroud)

当我改变

jsonOp.writeJson("\\src\\sample\\SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );
Run Code Online (Sandbox Code Playgroud)

进入

jsonOp.writeJson("SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );
Run Code Online (Sandbox Code Playgroud)

它不会给我任何错误,但是会在src文件夹之外创建文件。如何在示例文件夹中创建文件?

我的项目层次结构: WordToday>src>sample

Pau*_*ulo 7

尝试使用绝对路径。当您使用相对路径时,文件不会在您认为的文件夹内创建。

您可以尝试以下方法来检查您的相对路径在哪里:

Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);
Run Code Online (Sandbox Code Playgroud)

尝试一下并告诉我。

编辑:有关更多信息,请参阅:http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html


小智 5

在我的项目中,我使用文件定义在src文件夹之外创建了一个“ logs”文件夹:

File file = new File("logs/file.txt");
Run Code Online (Sandbox Code Playgroud)

因此,我希望您可以使用File(“ / file.txt”)在此处创建文件