Win a copy of React Cookbook: Recipes for Mastering the React Framework this week in the HTML Pages with CSS and JavaScript forum!
  • 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:
  • Campbell Ritchie
  • Ron McLeod
  • Paul Clapham
  • Rob Spoor
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Junilu Lacar
  • Tim Cooke
Saloon Keepers:
  • Tim Holloway
  • Piet Souris
  • Stephan van Hulst
  • Tim Moores
  • Carey Brown
Bartenders:
  • Frits Walraven
  • Himai Minh

BufferedWriter, PrinterWriter etc

 
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
 
Rancher
Posts: 43026
76
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
You can look at the javadocs of each of those classes to find the common -and the different- methods of each.
 
Vyas Sanzgiri
Ranch Hand
Posts: 686
Netbeans IDE Chrome Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Exactly where the confusion starts...there are so many combination. Is there a easier way to list it?
 
Ranch Hand
Posts: 324
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
That is a really big piece of pie for such a tiny ad:
the value of filler advertising in 2021
https://coderanch.com/t/730886/filler-advertising
reply
    Bookmark Topic Watch Topic
  • New Topic