2

I have a file (file.txt), and I need to empty his current content, and then to append some text multiple times.

Example: file.txt current content is:

aaa

bbb

ccc

I want to remove this content, and then to append the first time:

ddd

The second time:

eee

And so on...

I tried this:

// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.close();

// append
fileOut = new FileWriter("file.txt", true);

// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();

This works fine, but it seems inefficient because I open the file 2 times just for remove the current content.

HBv6
  • 3,487
  • 4
  • 30
  • 43

5 Answers5

7

When you open up the file to write it with your new text, it will overwrite whatever is in the file already.

A good way to do this is

// empty the current content
fileOut = new FileWriter("file.txt");
fileOut.write("");
fileOut.append("all your text");
fileOut.close();
Rob Wagner
  • 4,391
  • 15
  • 24
1

The first answer is not correct. If you create a new filewriter with the true flag for the second parameter, it will open in append mode. This will cause any write(string) commands to "append" text to the end of the file, not wipe out whatever text is already there.

namenamename
  • 195
  • 7
  • 1
    It's good that you have read my comments, but you are better off just editing the previous question. Answers should stand on their own, there is no need to refer to the "first" answer, especially since stackoverflow does not order questions that way. – Maarten Bodewes Jun 27 '12 at 20:00
  • I tried to do that but I don't see an "Add Comment" button on the answer. Is there something I am missing? – namenamename Jun 27 '12 at 20:03
  • You don't have enough reputation yet. – maba Jun 28 '12 at 14:03
1

I'm just stupid.

I only needed to do this:

// empty the current content
fileOut = new FileWriter("file.txt");

// when I want to write something I just do this multiple times:
fileOut.write("text");
fileOut.flush();

And AT THE END close the stream.

HBv6
  • 3,487
  • 4
  • 30
  • 43
  • Leave some time for stackoverflow to post answers Pierpaolo. This is a "gaming" site, don't answer your own question within the first day or so. – Maarten Bodewes Jun 27 '12 at 20:02
  • Sorry. Have I to remove my answer? – HBv6 Jun 27 '12 at 20:03
  • 1
    Nah, leave it be, the question is ok and answered by now, just leave some time for others to reply next time. – Maarten Bodewes Jun 27 '12 at 20:04
  • @owlstead: "don't answer your own question within the first day or so" Why not? – BoltClock Jun 27 '12 at 20:25
  • @BoltClock Well, people are writing answers as you are typing yours. They took the time to read it, think about it only to find out that the author posted a bit prematurely. It's not like this kind of question is not going to get answered correctly within the first 30 minutes or so (maybe a day is a bit long in this case) – Maarten Bodewes Jun 27 '12 at 20:28
0

I see that this question was answered quite a few Java versions ago... Starting from Java 1.7 and using the new FileWriter + BufferWriter + PrintWriter for appending (as recommended in this SO answer ), my suggestion for file erasing and then appending:

FileWriter fw = new FileWriter(myFilePath); //this erases previous content
fw = new FileWriter(myFilePath, true); //this reopens file for appending 
BufferedWriter bw = new BufferedWriter(fw);
PrintWriter pw = new PrintWriter(bw);
pw.println("text"); 
//some code ...
pw.println("more text"); //appends more text 
pw.flush();
pw.close();
Community
  • 1
  • 1
alisa
  • 1,336
  • 2
  • 15
  • 21
0

Best I could think of is :

Files.newBufferedWriter(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

and

Files.newInputStream(pathObject , StandardOpenOption.TRUNCATE_EXISTING);

In both the cases if the file specified in pathObject is writable, then that file will be truncated. No need to call write() function. Above code is sufficient to empty/truncate a file.This is new in java 8.

Hope it Helps

The Coder
  • 3,447
  • 7
  • 46
  • 81