简体   繁体   English

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

[英]How to create file in a project folder in Java?

in JsonOperation class: 在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();
    }
}

in my main calling class: 在我的主叫班上:

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

While running this, I got these errors: 运行此程序时,出现以下错误:

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)

When I change 当我改变

jsonOp.writeJson("\\src\\sample\\SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );

into 进入

jsonOp.writeJson("SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );

it doesn't give me any error but it creates the file outside the src folder. 它不会给我任何错误,但是会在src文件夹之外创建文件。 What should I do to create the file inside sample folder? 如何在示例文件夹中创建文件?

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

In my project i created a "logs" folder outside the src folder with the file definition: 在我的项目中,我使用文件定义在src文件夹之外创建了一个“ logs”文件夹:

File file = new File("logs/file.txt");

So I expect you can create a file there with File("/file.txt") 因此,我希望您可以使用File(“ / file.txt”)在此处创建文件

Try using the asbolute path. 尝试使用匹配路径。 When you use the relative path, the file is not being created inside the folder you think it is. 当您使用相对路径时,不会在您认为是的文件夹内创建文件。

You can try the following to check where your relative path is: 您可以尝试以下方法检查相对路径在哪里:

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

Try it and let me know. 试试看,让我知道。

EDIT: See this for more information: http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html 编辑:有关更多信息,请参见此: http : //docs.oracle.com/javase/tutorial/essential/io/pathOps.html

  1. For creating in any folder 用于在任何文件夹中创建

    File file = new File("C:\\\\Users\\\\YourUserName\\\\Directory\\\\fileName.txt");

  2. For in Project directory 对于项目目录

    File = new File("directory/fileName.txt");

  3. Check if file exists, if not create new 检查文件是否存在,如果不存在则创建新文件

    if (!file.exists()) { file.createNewFile(); }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM