• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • paul wheaton
  • Ron McLeod
  • Devaka Cooray
Sheriffs:
  • Jeanne Boyarsky
  • Liutauras Vilda
  • Paul Clapham
Saloon Keepers:
  • Tim Holloway
  • Carey Brown
  • Piet Souris
Bartenders:

Difference bet'n PrintWriter and FileWriter class.

 
Greenhorn
Posts: 25
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Bartender
Posts: 3323
86
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Rancher
Posts: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • 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: 2759
32
Eclipse IDE Spring Tomcat Server
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
If you look at the Writer javadocs, it lists all the classes that implement the Writer interface
 
Don't get me started about those stupid light bulbs.
reply
    Bookmark Topic Watch Topic
  • New Topic