| Author |
PrintWriter constructor can create file?
|
Charles Chikito
Ranch Hand
Joined: May 22, 2009
Posts: 76
|
|
Hi,
I have written the following code and it works.
If the PrintWriter constructor can be used to create a file and its println() method can be used to write into the file, why we need use it to wrap File or FileWriter objects... Can the PrintWriter object alone be used for file writing? Please advice.
Thanks
|
 |
Jesper de Jong
Java Cowboy
Bartender
Joined: Aug 16, 2005
Posts: 12907
|
|
|
The PrintWriter constructor that takes a String, and also the version that takes a File, creates the underlying FileWriter / FileOutputStream for you. It's not the PrintWriter itself that writes to the file directly; class PrintWriter just has a few constructors to make it more convenient for you, so that you don't have to create the underlying FileWriter object yourself.
|
Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 7 API documentation
Scala Notes - My blog about Scala
|
 |
Charles Chikito
Ranch Hand
Joined: May 22, 2009
Posts: 76
|
|
That helps me to understand better now.
Thanks
|
 |
Lucas Smith
Ranch Hand
Joined: Apr 20, 2009
Posts: 804
|
|
|
And remember that instantiating writers creates files physically!
|
SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
|
 |
 |
|
|
subject: PrintWriter constructor can create file?
|
|
|