Java - 方法未创建文件文本文件

Java - Method is not creating file text file

我得到了创建日志文件的方法:

public static void write(String path, String content) {
    try {
        FileWriter fw = new FileWriter(path, true);
        PrintWriter  buffWrite = new PrintWriter(fw);
        String row = "";
        DateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");  
        String now = dateFormat.format(new Date(System.currentTimeMillis()));
        row = "["+ now + "]\n" + content;
        buffWrite.println(row);
        buffWrite.close();
    } catch (IOException e) {
        e.printStackTrace();
    }
    
}

但是文件没有创建,而是出现了这个错误:

java.io.FileNotFoundException: src/main/resources/templates/log/email.log (No such file or directory)

解决这个问题的方法是什么?

尝试将 email.log 直接作为项目根文件夹的子文件夹和 src 的对等文件夹。

project_root/src

project_root/email.log(你的文件)

发生这种情况的原因可能是您的代码使用了相对路径,也许如果您改为放置整个路径,它也可以工作。