Java.io.ByteArrayInputStream.read() 方法


Java.io.ByteArrayInputStream.read() 方法

package com.codingdict;



import java.io.ByteArrayInputStream;

import java.io.IOException;



public class ByteArrayInputStreamDemo {



   public static void main(String[] args) throws IOException {

      byte[] buf = {65, 66, 67, 68, 69};

      ByteArrayInputStream bais = null;



      try {



         // create new byte array input stream

         bais = new ByteArrayInputStream(buf);



         int b =0;



         // read till the end of the stream

         while((b = bais.read())!=-1) {



            // convert byte to character

            char c = (char)b;



            // print

            System.out.println("byte :"+b+"; char : "+ c);



         }

         System.out.print(bais.read()+" Reached the end");



      } catch(Exception e) {



         // if I/O error occurs

         e.printStackTrace();

      } finally {

         if(bais!=null)

         bais.close();

      }   

   }

}