0

I am trying to clear the contents of a file I made in java. The file is created by a PrintWriter call. I read here that one can use RandomAccessFile to do so, and read somewhere else that this is in fact better to use than calling a new PrintWriter and immediately closing it to overwrite the file with a blank one.

However, using the RandomAccessFile to clear the file seems to be adding a string of null characters to the file (or perhaps it is the PrintWriter?) It only occurs if more text is added.

PrintWriter writer = new PrintWriter("temp","UTF-8");

while (condition) {
writer.println("Example text");

if (clearCondition) {
writer.flush();
new RandomAccessFile("temp","rw").setLength(0);
      //  Although the solution in the link above did not include ',"rw"'
      //  My compiler would not accept without a second parameter
writer.println("Text to be written onto the first line of temp file");
}
}
writer.close();

Running the equivalent of the above code is giving my temp file the contents:

^@^@^@^@^@^@^@^@^@^@^@^@^@Text to be written onto the first line of temp file

The number of null characters is equal to the number of characters erased (including newline characters). If no new text is added to the file, it is completely blank.

NOTE: writer needs to be able to write "Example Text" to the file again after the file is cleared. The clearCondition does not mean that the while loop gets broken.

EDIT: I have no idea what caused those null characters. I realized I am stupid and there was no need to have a temp file, just a temp String with all the data that would later be written to a file. Strings are super easy to reset with = ""; Thanks for all the suggestions

braX
  • 11,506
  • 5
  • 20
  • 33
ErikAGriffin
  • 739
  • 3
  • 10
  • 21
  • If you want create a temp file, you should probably use [File#createTempFile(String, String, File)](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String,%20java.io.File%29) or [File#createTempFile(String, String)](http://docs.oracle.com/javase/7/docs/api/java/io/File.html#createTempFile%28java.lang.String,%20java.lang.String%29). – Elliott Frisch Feb 01 '14 at 01:58
  • That still asks the question how can I write to it, then erase what was written if a condition is met, and afterwards still be able to write to it? – ErikAGriffin Feb 01 '14 at 03:08

1 Answers1

1

It doesn't seem a good idea to have an opened PrintWriter on the file and use a RandomAccessFile at the same time.

If you close your writer and open a new one on the file (instead of using RandomAccessFile) I think it will suit your needs.

dbermudez
  • 572
  • 3
  • 9
  • 1
    The only problem is that this needs to be done an unknown number of times. The premise is that if there is an error the file is erased and starts over. You can't know how many errors there are, so simply opening a new writer won't work – ErikAGriffin Feb 01 '14 at 03:07
  • That doesn't invalidate this answer in any way. You seem to be just creating objections for the sake of it frankly, both here and in your previous version of this question. – user207421 Feb 01 '14 at 03:16
  • Even if you need to use the writer an unknown number of times... I don't see the problem. Just use `writer.close(); writer=new PrintWriter("temp","UTF-8");` instead of `writer.flush(); new RandomAccessFile("temp","rw").setLength(0);` – dbermudez Feb 01 '14 at 08:36