How do I connect a Reader to an InputStream?


This is a very common question - after all, there aren't any SocketReaders, or PipedReaders. You need something to bridge the gap between a Reader, and an InputStream. That's where InputStreamReader comes into play.

InputStreamReader is a reader that can be connected to any InputStream - even filtered input streams such as DataInputStream, or BufferedInputStream. Here's an example that shows InputStreamReader in action.

// Connect a BufferedReader, to an InputStreamReader which is connected to
// an InputStream called 'in'.

BufferedReader reader = new BufferedReader ( new InputStreamReader ( in ) );

You can do the same with an OutputStream to a Writer (see OutputStreamWriter for more information).