Home  >  Core Java

java.io.ByteArrayInputStream Example in Java

By Arvind Rai, September 28, 2013
java.io.ByteArrayInputStream takes input as bye array and creates a buffer inside it. By the read method of ByteArrayInputStream, that buffer can be read. ByteArrayInputStream keeps a pointer inside it to handle next byte to read.

ByteArrayInputStreamTest.java
package com.concretepage.io;
import java.io.ByteArrayInputStream;
import java.io.IOException;
import java.io.InputStream;
public class ByteArrayInputStreamTest {
  public static void main(String[] args) {
      String str = "Hello World! \nThis is BufferedReader Program.";
      byte buff[] = str.getBytes();
      InputStream is = new ByteArrayInputStream(buff);
      try {
        while(is.available()!=0){
            System.out.print(new Character((char)is.read()));
        }
    } catch (IOException e) {
        e.printStackTrace();
    }finally{
        try {
            is.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
  }
}
 
Output:
Hello World! 
This is BufferedReader Program.
POSTED BY
ARVIND RAI
ARVIND RAI
FIND MORE TUTORILAS






©2019 concretepage.com | Privacy Policy | Contact Us