how to delete the content of text file without deleting itself

Solution 1:

Just print an empty string into the file:

PrintWriter writer = new PrintWriter(file);
writer.print("");
writer.close();

Solution 2:

I don't believe you even have to write an empty string to the file.

PrintWriter pw = new PrintWriter("filepath.txt");
pw.close();

Solution 3:

You want the setLength() method in the class RandomAccessFile.