Win a copy of Murach's Python Programming this week in the Jython/Python forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic

PrintWriter vs FileWriter  RSS feed

 
Sajee Joseph
Ranch Hand
Posts: 200
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello all,

I have a vector of strings.
I need to print these to a file each in a new line.
which would be a better approach for this among the 2 given below
1. Using PrintWriter
2. Using FileWriter.

Or is there is a better means?
 
Ernest Friedman-Hill
author and iconoclast
Sheriff
Posts: 24215
37
Chrome Eclipse IDE Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You must use a FileWriter, because that's the way to write into a file in Java. You may also, at the same time, use a PrintWriter as a wrapper for the FileWriter; it will give the FileWriter a more convenient interface.

 
Barry Gaunt
Ranch Hand
Posts: 7729
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Ernest, since 1.5 there is a constructor for PrintWriter that takes a String filename and another that takes a File. So I guess it's convenient now to use those instead of first creating a FileWriter with a PrintWriter chained onto it.

Sajee, Ernest is still correct, but the FileWriter is effectively hidden if you use the new constructors.
[ January 11, 2005: Message edited by: Barry Gaunt ]
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!