We help IT Professionals succeed at work.

Output to txt file

trout77x
trout77x asked
on
I want to output a string to the end of an existing text file.  I've tried using FileWriter but it simply overwrites the old file and saves only the new string.  I tried using RandomAccessFile but had some problems with it.  Whenever RandomAccessFile printed my string to the file it put a space in between every character. I also couln't figure out how to return a string to a new line using RandomAccessFile.  All help is appreciated.  
Comment
Watch Question

Commented:
Call seek(...) of RandomAccessFiles,

RandomAccessFiles ras = ...
ras.seek(ras.length());

This will move the cursor to the end of the file.

Commented:
>printed my string to the file it put a space in between every character
How do you write a string to a file by RandomAccessFile?
ras.writeUTF(string) shouldn't have such a probem.
Java Developer
CERTIFIED EXPERT
Top Expert 2010
Commented:
You need to open the FileWriter in append mode.

FileWriter writer = new FileWriter("file.txt", true);

CERTIFIED EXPERT
Top Expert 2004

Commented:
may this help?

FileOutputStream(String name, boolean append)

Creates an output file stream to write to the file with the specified name

Parameters:
name - the system-dependent file name
append - if true, then bytes will be written to the end of the file rather than the beginning

meikl ;-)

Author

Commented:
Thank you "objects" your post was all I needed.  To everyone else thank you as well.
Mick BarryJava Developer
CERTIFIED EXPERT
Top Expert 2010

Commented:
No worries :)
CERTIFIED EXPERT

Commented:
points to objects

Commented:
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:

-Points for  objects

Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
Venabili
EE Cleanup Volunteer

Explore More ContentExplore courses, solutions, and other research materials related to this topic.