Connecting Tech Pros Worldwide Forums | Help | Site Map

Append to exsisting text file

Dave W
Guest
 
Posts: n/a
#1: Jul 18 '05
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



Luca Paganelli
Guest
 
Posts: n/a
#2: Jul 18 '05

re: Append to exsisting text file


See

public PrintWriter(Writer out,
boolean autoFlush)

--
Luca Paganelli
ICQ# 52629494


Fahd Shariff
Guest
 
Posts: n/a
#3: Jul 18 '05

re: Append to exsisting text file


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..."
Dave W
Guest
 
Posts: n/a
#4: Jul 18 '05

re: Append to exsisting text file


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" <fahdshariff@yahoo.com> wrote in message
news:9bc0209f.0405120114.1e0ebe5c@posting.google.c om...[color=blue]
> 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..."[/color]


Dave W
Guest
 
Posts: n/a
#5: Jul 18 '05

re: Append to exsisting text file


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();
}


Fahd Shariff
Guest
 
Posts: n/a
#6: Jul 18 '05

re: Append to exsisting text file


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
Closed Thread