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

3

在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();
    }
}

在我的主调用类中:
    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) );

运行代码时,我遇到了如下错误:
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)

当我改变时
jsonOp.writeJson("\\src\\sample\\SaveData.json", jsonOp.toJsonObj("dateToday", dateString) );

转换为

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

程序没有报错,但是它在src文件夹之外创建了该文件。我应该怎么做才能在sample文件夹内创建文件?

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


1
我刚意识到 sample 实际上是一个Java包。但在我的文件资源管理器中它显示为文件夹。 - aeruhxi
Java项目中的包实际上是位于文件夹中的。这就是Java的工作方式。但我强烈建议不要在项目中进行编写,因为这将使其依赖于源代码树的可用性。请使用可配置的外部位置。 - Dave Newton
4个回答

9
在我的项目中,我创建了一个"logs"文件夹,它在src文件夹之外,并包含以下文件定义:
File file = new File("logs/file.txt");

所以我希望你可以使用 File("/file.txt") 在那里创建一个文件。

谢谢。我想我会选择这个。 - aeruhxi
是的,确实如此。但我不明白为什么我不能在src文件夹内创建文件。 - aeruhxi
然后执行 File(/src/file.txt) 命令,你就在 src 目录下了。 - rapgru

7
尝试使用绝对路径。当使用相对路径时,文件并未被创建在您所认为的文件夹内。
您可以尝试以下方法来检查相对路径所在位置:
Path currentRelativePath = Paths.get("");
String s = currentRelativePath.toAbsolutePath().toString();
System.out.println("Current relative path is: " + s);

试一试并让我知道。

编辑: 查看此链接以获取更多信息:http://docs.oracle.com/javase/tutorial/essential/io/pathOps.html


相同错误。我只能在 src 文件夹之外创建文件。 - aeruhxi

4
  1. 在任何文件夹中创建

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

  2. 在项目目录中创建

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

  3. 检查文件是否存在,如果不存在则创建新的

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


0

这取决于您是否正在开发一个应用程序到生产级别,如果是本地开发,这些东西基本上可以工作,但否则您将会被卡住很长时间,首先尝试通过输入以下命令来了解您的服务器文件夹使用情况:

Path currentPathPosition = Paths.get("");
ResponseEntity.ok(currentPathPoosition.getAbsolutePath());

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接