0

i just learned File Handling yesterday . There is a confusion about classes usage for me . For Example some of the examples were like this

  Paths  myfile= paths.get("c:\\Sample.txt");
  OutputStream  opt= new  BufferedOutputStream(myfile.hew OutputStream(CREATE));
  BufferedWriter br= new BufferedWriter( new OutputStreamWriter(opt));
  br.write();.....  ;       

and so on 

This is one way and Somewhere i find

  BufferedWriter bf = new BufferedWriter( new FileWriter("c:\\Sample.txt"));
  bf.write("Hello  File Handling ");'

i am not concerned if in above code it is creating file and second one is writing . i want to ask

in First one we are using parent (OutputStream)class and then code goes ,
and in second one we use BufferedWriter Directly instead of using Parent one , So what's the logic to do so , i mean does it make any difference if we use parent classes while doing something big or we can go for any logic

Thanks for your help

Sikander
  • 2,799
  • 12
  • 48
  • 100

1 Answers1

0

It's a bit unclear what you're asking, but the Java IO classes use the Decorator pattern to achieve different ways of reading and writing from an Input or Output Stream. What this allows is flexibility with how data is loaded and written, since each one allows you to decorate the current type.

Decorator Pattern for IO

Decorator Pattern

Community
  • 1
  • 1
tjg184
  • 4,508
  • 1
  • 27
  • 54