Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Difference bet'n PrintWriter and FileWriter class.

 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I got confused when i tried following code.

try{

File file = new File("write.txt");
FileWriter writer = new FileWriter(file);

PrintWriter printWriter = new PrintWriter(writer);
printWriter.println("pqr");
printWriter.println("jkl");
printWriter.close();

PrintWriter printWriter = new PrintWriter(file);
printWriter.println("abc");
printWriter.println("xyz");
printWriter.close();
}

what is the basic Difference Between following PrintWriter(File file) and PrintWriter(FileWriter writer) need example to explain
and in what scenario i should use these classes
 
Tony Docherty
Bartender
Posts: 2969
59
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
what is the basic Difference Between following PrintWriter(File file) and PrintWriter(FileWriter writer)

The PrintWriter(File file) constructor creates a PrintWriter object that writes to the specified file.
The PrintWriter(FileWriter writer) constructor doesn't exist but there is a PrintWriter(Writer writer) constructor which creates a PrintWriter object that writes to the specified Writer which in your code is a FileWriter.

and in what scenario i should use these classes

The PrintWriter(File file) constructor is provided as a shortcut to save you having to create you own FileWriter. The PrintWriter(Writer writer) constructor can be used to pass in any type of Writer and so can be used where you don't know/care what type of Writer you will be writing to. Generally speaking, if you are creating the PrintWriter to write to a file and have a File object (or file name) then use the PrintWriter(File file) constructor otherwise use the PrintWriter(Writer writer) constructor.
 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You will see this pattern where Writers/Readers are constructed using other Writer/Readers all over the java.io package. This is called a Decorator pattern. It allows you to mix and match functionality as you see fit. So, for example, if you want to print to a file you can do this



If you want to write to a Pipe you do this




If you want to use bufferring before writing to a file you do




If they didn't have a decorator pattern, they would had to implement lot of classes that have all these combinations
 
Nakul P. Patel
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
@ Tony Docherty:"The PrintWriter(Writer writer) constructor can be used to pass in any type of Writer and so can be used where you don't know/care what type of Writer you will be writing to."

Can you please tell me what are different types of Writer in java.io package?
If possible please write small line of code,it will help me(us) to understand in better way.



 
Jayesh A Lalwani
Rancher
Posts: 2756
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If you look at the Writer javadocs, it lists all the classes that implement the Writer interface
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic