2
FileWriter fwriter = new FileWriter("C:/Users/NULL NULL NULL/Desktop/New Text Document.txt", false); // set the write to false so we dont append to it

BufferedWriter write = new BufferedWriter(fwriter); // this is the type so we can write to it.

write.write(""); // write empty string to delet everything

write.close(); // close the buffer to gain back memory space that the buffer 

took up.

YCF_L
  • 51,266
  • 13
  • 85
  • 129
Abdul Sheikh
  • 151
  • 2
  • 6
  • First what is the question ? Second have you tested the code you have pointed ? – Alexander Petrov Jul 17 '16 at 21:00
  • 8
    I believe this is duplicate of this: [http://stackoverflow.com/questions/6994518/how-to-delete-the-content-of-text-file-without-deleting-itself](http://stackoverflow.com/questions/6994518/how-to-delete-the-content-of-text-file-without-deleting-itself) – Alexander Petrov Jul 17 '16 at 21:01

1 Answers1

6

How about this:

File file = new File("myfile");
if(file.exists()){
    file.delete();
}
file.createNewFile();

Or this, which will create a new file OR overwrite existing one:

Files.newBufferedWriter(Paths.get("myfile"));

Or @CandiedOrange's PrinterWriter:

new PrintWriter("myfile").close();

Otherwise, the good old way:

File file = new File("myfile");
FileOutputStream fooStream = new FileOutputStream(file, false);

fooStream.write("".getBytes());
fooStream.close();
alexbt
  • 15,503
  • 6
  • 73
  • 85