• Post Reply Bookmark Topic Watch Topic
  • New Topic

Write somethin' out without overwriting  RSS feed

 
Jose Campana
Ranch Hand
Posts: 339
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You need to open the file for appending -- otherwise, it will overwrite the previous file data.



Henry
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
This will work too...



If you find another solution, please let me know.

Henry
 
Henry Wong
author
Sheriff
Posts: 23283
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Rob Spoor
Sheriff
Posts: 21092
85
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Jose Campana
Ranch Hand
Posts: 339
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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 Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!