-3

I want my code to empty a specific text file, when it is the beginning of each month. I have this code but it doesn't seem to work, do I have something wrong with the syntax maybe?

public void monthlyCleanse(File f){

    LocalDate localDate = LocalDate.now();

    String d = DateTimeFormatter.ofPattern("dd").format(localDate);

    System.out.println(d);

    if(d.equals("01")){


        "filename.txt".replace(toString().valueOf(f), "");
    }
}

As you can see I am changing the format to only show the day of the month, then I am checking if that day equals "01" then do something...which is to empty a text file.

Then i am calling it here...

    monthlyCleanse(new File("filename.txt"));

How can i make sure that this file is emptied?

Maiwand
  • 73
  • 8
  • 6
    Why convert to a string? Just use `localDate.getDayOfMonth() == 1`. – Andy Turner Sep 08 '17 at 10:14
  • 3
    Also: what do you think `"filename.txt".replace(toString().valueOf(f), "");` does? – Andy Turner Sep 08 '17 at 10:15
  • @Nathan My point was that it doesn't make sense ... but Andy T. already point it out. – AxelH Sep 08 '17 at 10:19
  • @AndyTurner I assumed that It would replace the contents of the "filename.txt" file with "". Therefore making it empty? – Maiwand Sep 08 '17 at 10:22
  • @Maiwand well, yes, but you then immediately discard the empty string. So that statement is effectively a no-op. Besides that, replacing the *name* of a file with an empty string doesn't make that file empty. – Andy Turner Sep 08 '17 at 10:23
  • @AndyTurner so what would you say I should put within the if condition? So if the date does == 1. what should I use? – Maiwand Sep 08 '17 at 10:25
  • @Maiwand You are not replacing the **contents** of the file with `""`. you're replacing a `String`. You should refer to this question that explains how to write in a file: https://stackoverflow.com/questions/2885173/how-do-i-create-a-file-and-write-to-it-in-java – Turtle Sep 08 '17 at 10:29
  • Sorted thanks lads :) – Maiwand Sep 08 '17 at 10:29
  • 3
    This is a very confused question. The title is about doing something monthly. But you've solved that part (albeit in a weird way). Your actual question is about emptying a file, which would have the same answer regardless of when you do it. – slim Sep 08 '17 at 10:30

1 Answers1

1

When you do this "filename.txt".replace(toString().valueOf(f), "");, it returns a modified copy of the contents of the String "filename.txt", not the file. You should refer to this question that explains how to delete the contents of a file.

This could look like this:

if(d.equals("01")){
    PrintWriter writer = new PrintWriter(f);
    writer.print("");
    writer.close();
}

On a side note: When you call: toString().valueOf(f), it is quite confusing. You should replace it by String.valueOf(f), as it is a static function. If you look at its implementation, it returns the path of the File passed in parameter, so it has no connection with an already existing String object.

Turtle
  • 1,626
  • 16
  • 26