Forums Register Login

Write somethin' out without overwriting

1
+Pie Number of slices to send: Send
Hiya!

How's everybody doing today at the ranch?
I have a query about IO, since that's what I'm currently practicing.

What's the method that allows me to write out a String to a file without over-writing the contents of the file, but rather place the String in a new line?

Here's some code:



The preceding code over-writes whatever is in the file with the String that's passed as an argument to the println method. I can't find the appropriate method to print out a String as a new Line of text in the text file.

How can I accomplish this? Can anybody help me?

Jose
+Pie Number of slices to send: Send
You need to open the file for appending -- otherwise, it will overwrite the previous file data.



Henry
+Pie Number of slices to send: Send
Thanks Mr. Wong,

Do you Know if there's a way to do that without using a FileOutputStream object?

I'd prefer to stick to PrintWriter only. Is it possible?

Or is there a Constructor for class File that allows this, I mean open the file for appending?

Thanks to y'all,
Jose
+Pie Number of slices to send: Send
This will work too...



If you find another solution, please let me know.

Henry
+Pie Number of slices to send: Send
 

Or is there a Constructor for class File that allows this, I mean open the file for appending?



You do know that you can look it up yourself... http://java.sun.com/j2se/1.5.0/docs/api/java/io/File.html

Henry
+Pie Number of slices to send: Send
Mr Wong !
Thank you for the replies fella.
It seems [according to me] that in order to open a File for appending you must chain the PrintWriter with a FileWriter, just as you described....
I'd to like to know if there's a way to do it with the PrintWriter only, but apparently there's not.
So, thank very much Mr. Wong Sir.

If anybody knows another solution please post it...
Good luck and prosperity,
Sincerely,

Jose
+Pie Number of slices to send: Send
 

Originally posted by Jose Campana:
I'd to like to know if there's a way to do it with the PrintWriter only, but apparently there's not.


No there isn't. You know why? Because even your solution that overwrites the contents uses a FileWriter (actually a FileOutputStream and OutputStreamWriter).

Here's the Java 6 code of the constructor you're using:

In other words: the File is wrapped in a FileOutputStream, that is wrapped in an OutputStreamWriter, that is wrapped in a BufferedWriter.

Of course this could be shorter:


The PrintStream constructors that take a file or string are merely convenience constructor that were added in Java 5. Prior to this, you had to use a FileWriter or FileOutputStream.
+Pie Number of slices to send: Send
Whoa.
That's really something Mr. Prime. sometimes I forget just how many things are implicit in Java.
Thanks for your post, It's been really useful for a rookie like myself.
Keep up the amazing work you do everyday here.
May the fortune be with you.
Sincerely,

Jose
We cannot change unless we survive, but we will not survive unless we change. Evolving tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com


reply
reply
This thread has been viewed 10062 times.
Similar Threads
Input from console, writing to file
PrintWrite close() does not catches Exception
formatting get lost after streaming
How to read data from a text file
Getting the URL by crawling whose content-type is not text/html
More...

All times above are in ranch (not your local) time.
The current ranch time is
Mar 29, 2024 08:14:23.