Skip to content
Advertisement

Java – Method is not creating file text file

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?

Advertisement

Answer

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.

6 People found this is helpful
Advertisement