File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes Programmer Certification (SCJP/OCPJP) and the fly likes BufferedWriter, PrinterWriter etc Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
JavaRanch » Java Forums » Certification » Programmer Certification (SCJP/OCPJP)
Bookmark "BufferedWriter, PrinterWriter etc" Watch "BufferedWriter, PrinterWriter etc" New topic
Author

BufferedWriter, PrinterWriter etc

Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

I am confused between the different methods available in I/O and their combination

1) PrintWriter pw = new PrintWriter(new FileWriter("text"));
2) BufferedWriter bw = new BufferedWriter(new FileWriter("text"));

Can someone help me list out all the possible combinations in the case of readers and writers?

Thanks


===Vyas Sanzgiri===
My Blog
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 39547
    
  27
You can look at the javadocs of each of those classes to find the common -and the different- methods of each.


Ping & DNS - updated with new look and Ping home screen widget
Vyas Sanzgiri
Ranch Hand

Joined: Jun 16, 2007
Posts: 686

Exactly where the confusion starts...there are so many combination. Is there a easier way to list it?
Himalay Majumdar
Ranch Hand

Joined: Sep 28, 2008
Posts: 324
For the exam you need to know

FileReader
BufferedReader
FileWriter
BufferedWriter
PrintWriter

Readers and Writers are used to write characters. Writing bytes is done by streams which is not in exam.

Without Using Readers and Writer you can create a file like this



Using FileWriter and FileReader



To Read an object, create a character array to store the input read by the file..as below.



Notice, while writing data we manually used line separators in fw.write("hello\nworld");
Even while reading, we put it into character array, where we need to declare the size intially.

So..usually reader and writer are wrapped and then used. Its a less painful way of writing and reading data to and from file.




Instead of using a BufferedWriter we can use a PrintWriter instead.it has println() method..that is more easier to write.

You just need to repalce the BufferedWriter code above with




To read using BufferedReader



Classes being used to help each other in this manner can be seen in IO extensively, also known as Decorator Pattern.

SCJP 1.6, SCWCD 5.0, SCBCD 5.0 [loading..]
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: BufferedWriter, PrinterWriter etc
 
Similar Threads
reading files
constructing a PrintWriter object
Heap size increases when writing XML to the File
Is PrintWriter Buffered?
Printwriter vs Bufferedwriter