Java.io.ByteArrayInputStream.read len() 方法


Java.io.ByteArrayInputStream.read len() 方法

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);



         // create buffer

         byte[] b = new byte[4];

         int num = bais.read(b, 2, 2);



         // number of bytes read

         System.out.println("Bytes read: "+num);



         // for each byte in a buffer

         for (byte s :b) {



            // covert byte to char

            char c = (char)s;



            // prints byte

            System.out.print(s);



            if(s == 0)



               // if byte is 0

               System.out.println(": Null");

            else



               // if byte is not 0

               System.out.println(": "+c);

         }



      } catch(Exception e) {



         // if I/O error occurs

         e.printStackTrace();

      } finally {

         if(bais!=null)

            bais.close();

      }   

   }

}