aspose file tools*
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 Spring in Action this week in the Spring 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
Author

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

drew taylor
Greenhorn

Joined: Feb 20, 2006
Posts: 21
Hi,

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
out.append(textBox.getText());
out.println();
out.close();

I have also tried:

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

Any suggestions?

Thanks,
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24187
    
  34

You haven't shown us what sort of object "out" is, with its "append" method. I'm guessing it's an instance of java.io.FileWriter; 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.write("Hello");
out.close();
out = new FileWriter("myfile.txt", true);
out.write(", world!");
out.close();

The file contains "Hello, world!"


[Jess in Action][AskingGoodQuestions]
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19719
    
  20

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 ]

SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
drew taylor
Greenhorn

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
Greenhorn

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?

Thanks,

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

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

Joined: Oct 13, 2005
Posts: 39408
    
  28
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
Bartender

Joined: Oct 02, 2003
Posts: 11408
    
  16

Nathan,

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
Greenhorn

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

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

Why not try it to see what happens?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to append text to the end of a text file?