Two Laptop Bag*
The moose likes Beginning Java and the fly likes How to append text to the end of a text file? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login

Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How to append text to the end of a text file?" Watch "How to append text to the end of a text file?" New topic

How to append text to the end of a text file?

drew taylor

Joined: Feb 20, 2006
Posts: 21

Can anyone see why the code below doesn't append (text entered by user in GUI) to the end of the text file? It just overwrites the original text in the file:

// Write text to file

I have also tried:

out.append(textBox.getText() + '\n'); but the original text was still overwritten.

Any suggestions?

Ernest Friedman-Hill
author and iconoclast

Joined: Jul 08, 2003
Posts: 24166

You haven't shown us what sort of object "out" is, with its "append" method. I'm guessing it's an instance of; in that case, note that append() is exactly the same as write() -- it does nothing special.

What matters is not whether you call append(), but how you construct the FileWriter. There's a constructor that takes a boolean argument after the filename; if you pass "true" for that argument, then the FileWriter appends to the end of the file. If you pass false, or use any other constructor, then the FileWriter erases the prior contents of the file the instant the FileWriter is created.

So if you do something like

FileWriter out = new FileWriter("myfile.txt");
out = new FileWriter("myfile.txt", true);
out.write(", world!");

The file contains "Hello, world!"

[Jess in Action][AskingGoodQuestions]
Rob Spoor

Joined: Oct 27, 2005
Posts: 19541

The append method just writes to the writer.

The trick is in creating the FileWriter. There are two constructors that take a boolean. If this boolean is true, everything you write to that file is appended. If it is false, or you use a constructor without the boolean, you will overwrite the file.

In other words, just what Ernest has just said
[ April 06, 2008: Message edited by: Rob Prime ]

How To Ask Questions How To Answer Questions
drew taylor

Joined: Feb 20, 2006
Posts: 21
Thanks guys, I just added true after the filename to write to and it worked
[ April 06, 2008: Message edited by: drew taylor ]
drew taylor

Joined: Feb 20, 2006
Posts: 21
I'm trying to read the data from the text file, but the following code only seems to read the first line, why is this?


try {
File file = new File(fileName);
FileReader reader = new FileReader(file);
BufferedReader in = new BufferedReader(reader);
String string;
while ((string = in.readLine()) != null) {
} catch (IOException e) {
Nathan Coffey

Joined: May 21, 2012
Posts: 1
To read it
Campbell Ritchie

Joined: Oct 13, 2005
Posts: 36478
Nathan Coffey, welcome to the Ranch

I would have added code tags to your post, which would have improved its appearance, but you haven’t indented the code you posted.
Oh, it’s only short; I’ll indent it for you this time.
fred rosenberger
lowercase baba

Joined: Oct 02, 2003
Posts: 10911


Also note that the original post and last update before yours is from four years ago. I doubt they are still waiting for an answer.

There are only two hard things in computer science: cache invalidation, naming things, and off-by-one errors
Diego Dietdricks

Joined: Nov 04, 2013
Posts: 1
Will this work when you add data to a blank text file?
Tony Docherty

Joined: Aug 07, 2007
Posts: 1939
Welcome to the Ranch.

Why not try it to see what happens?
It is sorta covered in the JavaRanch Style Guide.
subject: How to append text to the end of a text file?
Similar Threads
editing a line of a txt file with a thousands of line
Line Feed Carriage Return problem
Delete text when using RandomAccessFile
add text to a .txt file