Converting an InputStream to a Reader

An InputStream is byte oriented. A Reader is Character oriented. To hook up an InputStream to a Reader and have the conversion done automatically for you, use the class InputStreamReader.

Main.java:

import java.io.*;
 
public class Main {   
   public static void main(String[] args) throws Exception {
      byte[] b = new byte[] { 65, 66, 67 };
 
      ByteArrayInputStream bais = new ByteArrayInputStream(b);
      InputStreamReader isr = new InputStreamReader(bais);
      int k;
      while ((k = isr.read()) > -1) {
         System.out.println((char) k);
      }
   }
}

outputs:

A
B
C