Help coderanch get a
new server
by contributing to the fundraiser
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

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

 
Greenhorn
Posts: 21
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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,
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!"
 
Sheriff
Posts: 22791
131
Eclipse IDE Spring Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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();
}
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
To read it
 
Marshal
Posts: 79531
379
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
lowercase baba
Posts: 13091
67
Chrome Java Linux
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Will this work when you add data to a blank text file?
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Welcome to the Ranch.

Why not try it to see what happens?
 
Today's lesson is that you can't wear a jetpack AND a cape. I should have read this tiny ad:
We need your help - Coderanch server fundraiser
https://coderanch.com/t/782867/Coderanch-server-fundraiser
reply
    Bookmark Topic Watch Topic
  • New Topic