This week's giveaway is in the Spring forum.
We're giving away four copies of Learn Spring Security (video course) and have Eugen Paraschiv on-line!
See this thread for details.
Win a copy of Learn Spring Security (video course) this week in the Spring forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

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

 
drew taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24208
35
Chrome Eclipse IDE Mac OS X
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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!"
 
Rob Spoor
Sheriff
Pie
Posts: 20492
54
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 ]
 
drew taylor
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 21
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
To read it
 
Campbell Ritchie
Sheriff
Posts: 48363
56
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 12083
29
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Diego Dietdricks
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Will this work when you add data to a blank text file?
 
Tony Docherty
Bartender
Posts: 2936
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why not try it to see what happens?
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic