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

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

在我的主调用类中:

    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 文件夹之外创建文件。我应该怎么做才能在示例文件夹中创建文件?

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

最佳答案

在我的项目中,我在 src 文件夹外部创建了一个“logs”文件夹,其中包含文件定义:

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

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

关于java - 如何在Java中的项目文件夹中创建文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35017393/

相关文章:

java - 设置处理 vector 的恒定速度

java - Hibernate 为新的子实体生成 ghost 条目

java - 无法向 openfire 服务器发送消息

java - ThreadPoolExecutorService 顺序执行线程而不是并发执行线程?

java - 使用jsoup下载文件,响应bodyStream只能下载一兆字节的文件

Java Jsoup无法选择表

java - 从 Procfile 读取 Amazon Elastic Beanstalk 环境的变量

java - 在Android中合并两个监听器的数据

java - 使用 Java 运行时执行命令

java - 我应该如何实现这个 HashMap 的 equals 和 hashCode 方法来表示自动机状态?