aspose file tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes PrintWriter constructor can create file? Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Reply Bookmark "PrintWriter constructor can create file?" Watch "PrintWriter constructor can create file?" New topic
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
    
    3

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
    
    1

And remember that instantiating writers creates files physically!


SCJP6, SCWCD5, OCE:EJBD6.
BLOG: http://leakfromjavaheap.blogspot.com
 
I agree. Here's the link: http://zeroturnaround.com/jrebel - it saves me about five hours per week
 
subject: PrintWriter constructor can create file?
 
Similar Threads
move file A to file B
How can PrintWriter know we have reached end of page?
PrintWriter can only write 130k?
Printing Int Array to File using PrintWriter
from where should i import com.sun.xml.tree.XmlDocumentBuilder;