By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,034 Members | 822 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,034 IT Pros & Developers. It's quick & easy.

Append to exsisting text file

P: n/a
Hi,

I'm hoping for some guideance. I'm trying append to the bottom of a text
file using Java.

Any advice, further reading would be greatly appreciated!

Cheers,
Dave
Jul 17 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
See

public PrintWriter(Writer out,
boolean autoFlush)

--
Luca Paganelli
ICQ# 52629494
Jul 17 '05 #2

P: n/a
See

FileWriter
public FileWriter(String fileName,
boolean append)
throws IOException

Constructs a FileWriter object given a file name with a boolean
indicating whether or not to append the data written.

Sample code:

BufferedWriter writer = new BufferedWriter(
new FileWriter("myfile.txt",true)) ;
writer.write("HELLO") ;
writer.close() ;

Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."
Jul 17 '05 #3

P: n/a
Thanks for that, but i'm hoping you can help me with my syntax. It
currently makes a new text file/replaces but i'm unsure how to append an
already exsisting one.

This is what i have:

try
{
String outName = "out.txt";
FileWriter out = new FileWriter(outName);
writer.write(total);
writer.newLine();
writer.close();
}

Thanks,
Dave

"Fahd Shariff" <fa*********@yahoo.com> wrote in message
news:9b**************************@posting.google.c om...
See

FileWriter
public FileWriter(String fileName,
boolean append)
throws IOException

Constructs a FileWriter object given a file name with a boolean
indicating whether or not to append the data written.

Sample code:

BufferedWriter writer = new BufferedWriter(
new FileWriter("myfile.txt",true)) ;
writer.write("HELLO") ;
writer.close() ;

Fahd Shariff
http://www.fahdshariff.cjb.net
"Let the code do the talking..."

Jul 17 '05 #4

P: n/a
Sorry, let me correct that:

try
{
//ask user for file name to write to

FileWriter out = new FileWriter("out.txt");
BufferedWriter writer = new BufferedWriter(out);
writer.write(total);
writer.close();
}
Jul 17 '05 #5

P: n/a
Change to :
FileWriter out = new FileWriter("out.txt", true);

If the file does not exist it is created and if it does, text is appended to it.

Fahd Shariff
http://www.fahdfshariff.cjb.net
Jul 17 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.