writing at the last line of a text file

J

Jack Vamvas

I am running the following code:
try{
DataOutputStream out2 = new DataOutputStream(new
BufferedOutputStream(
new FileOutputStream("C:\\mytext.txt")));
out2.write("Line1:");

} catch(IOException e) {
throw new RuntimeException(e);
}

The problem I have is how do I add the text at the end of the existing
text.For example,if there are already 3 lines in mytext.txt how can I make
my entry the fouth line?


Jack
___________________________________________________________________
Remotely manage MS SQL db with SQLdirector -
www.ciquery.com/tools/sqldirector/
 
K

Kristian Bisgaard Lassen

I am running the following code:
try{
DataOutputStream out2 = new DataOutputStream(new
BufferedOutputStream(
new FileOutputStream("C:\\mytext.txt")));
out2.write("Line1:");

} catch(IOException e) {
throw new RuntimeException(e);
}

The problem I have is how do I add the text at the end of the existing
text.For example,if there are already 3 lines in mytext.txt how can I make
my entry the fouth line?

Hi Jack,

I have not tried this but the Java API have a contructor (in the 1.4.2 API
anyway, but it should be in older versions) FileOutputStream (String name,
boolean append). I guess you should try DataOutputStream out2 = new DataOutputStream(new
BufferedOutputStream(new FileOutputStream("C:\\mytext.txt", true)))
instead.

Best Regards
Kristian
 
M

Marko Lahma

How about:

public FileOutputStream(File file,
boolean append)
throws FileNotFoundException

"Creates a file output stream to write to the file represented by the
specified File object. If the second argument is true, then bytes will
be written to the end of the file rather than the beginning. A new
FileDescriptor object is created to represent this file connection."

This might be what you are looking for. And maybe you should wrap it
with PrintWriter for text output.

-Marko
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,444
Messages
2,571,709
Members
48,796
Latest member
Greg L.
Top