• Post Reply Bookmark Topic Watch Topic
  • New Topic

Stream Reader  RSS feed

 
Phillipe Rodrigues
Ranch Hand
Posts: 165
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,
What is the difference between the InputStream,OutputStream and InputStreamReader,OutputStreamWriter and DataInputStream,DataOutputStream
.I wentt through the API doc but did not get DataInputStream(
(confused with them)

Suppose i a text file exist with text "My Name is 123 phillipe"

So in what situations can i use the above io classes.

Please help
 
Rob Spoor
Sheriff
Posts: 20903
81
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
InputStream and OutputStream are generally for handling binary files. That's why they handle bytes. Reader and Writer are the equivalent for text files - they handle chars instead of bytes. The difference is that some encodings, like UTF-8, use multiple bytes for characters.


Sometimes you are given an InputStream / OutputStream but you would rather have a Reader / Writer. That's where InputStreamReader / OutputStreamWriter come in. They wrap the InputStream / OutputStream into a Reader / Writer object - anything read from an InputStreamReader is actually read from the backing InputStream, converting if the encoding requires it. Similarly, anything written to an OutputStreamWriter is actually written to the backing OutputStream, converting if the encoding requires it.


DataInputStream and DataOutputStream can handle different contents. Just check the methods - instead of reading / writing a byte / character, you can read / write full longs, doubles, anything. That way, you don't have to worry about the converting.
In fact, you could switch to ObjectInputStream / ObjectOutputStream as it can do what DataInputStream / DataOutputStream can do, plus handle reading / writing Serializable objects.
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!