• Post Reply Bookmark Topic Watch Topic
  • New Topic

why this???

 
omar salem
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi everyone....i am new to the input output issue in java.....could anyone please explain this statement to me ( BufferedReader br = new BufferedReader(new InputStreamReader(System.in))...why do we have to wrap an InputStreamReader object inside the BufferedReader and what will happen if i wrap an InputStream object instead of InputStreamReader....would appreciate your help....many thanks in advance.
 
Joe Ess
Bartender
Posts: 9362
11
Linux Mac OS X Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Originally posted by omar salem:
why do we have to wrap an InputStreamReader object inside the BufferedReader


To convert the InputStream that System exposes in to a Reader.

Originally posted by omar salem:
and what will happen if i wrap an InputStream object instead of InputStreamReader


You'd get a compile error because BufferedReader expects a Reader.
It seems like you have to jump through a lot of hoops to get IO to work, but once you get the hang of it, it is extremely powerful. Java uses the same classes to read and write to anything (file, socket, database) so once you've figured it out, for one, you can read/write everywhere. Joining simple streams together can get us some very complex behavior, for example, there's streams that compress data and streams that read and write objects. You just need to put another class in the chain.
If you haven't already, browse the Java Tutorial IO chapter.
 
omar salem
Ranch Hand
Posts: 34
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks a million joe......i reall appreciate your help.....
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!