0

I got this method for creating a log 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();
    }
    
}

but the file is not created, instead I get this error:

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

Whats the fix for this?

arksdf
  • 363
  • 1
  • 2
  • 13
  • 1
    Does the directory `src/main/resources/templates/log` exist? It's a relative path, so it will be relative to whatever the current directory is at the time. – Jim Garrison Jan 18 '22 at 18:46
  • Warning: **don't use `Date` and `SimpleDateFormat`** — they're [obsolete and troublesome](https://stackoverflow.com/q/1969442/507738). Use classes from the `java.time` package instead. In your case, use `LocalDateTime` and `DateTimeFormatter`. – MC Emperor Jan 18 '22 at 20:37

1 Answers1

1

Try putting the email.log directly as a child of the project root folder and a peer of src.

project_root/src

project_root/email.log (your file)

This is probably happening because of the relative path usage on your code, maybe if you place the entire path instead, it could work too.