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.
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,
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.
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
Post by:autobot
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