Byte Streams in java:
Programs use byte streams to perform input and output of 8-bit bytes. All byte stream classes are descended from InputStream and OutputStream.
Always close the files to avoid serious resource leaks.
Byte streams should only be used for the most primitive I/O. It represents a kind of low-level I/O so you should avoid in case complicated data types like text or graphics.
ByteArrayInputStream:
A ByteArrayInputStream class provides an internal buffer for the bytes read from input stream.
Commonly used constructors of ByteArrayInputStream:
1. ByteArrayInputStream(byte[] buf)
Creates a ByteArrayInputStream so that it uses buf as its buffer array.
2. ByteArrayInputStream(byte[] buf, int offset, int length)
Creates ByteArrayInputStream that uses buf as its buffer array.
Commonly used methods of ByteArrayInputStream:
1. public int read()
This method reads the next byte of data from the InputStream. Returns an int as the next byte of data. If it is end of file then it returns -1.
Example:
ByteArrayInputStreamExample.java
import java.io.ByteArrayInputStream; /** * This program is used to read byte array * input using ByteArrayInputStream. * @author w3schools */ class IOTest{ String str = "www.w3schools.com"; public void readFile(){ try { //Converting string into byte array. byte b[] = str.getBytes(); //Creating ByteArrayInputStream object. ByteArrayInputStream bais = new ByteArrayInputStream(b); int i; //read file. while((i=bais.read())!=-1){ System.out.print((char)i); } } catch (Exception e) { e.printStackTrace(); } } } public class ByteArrayInputStreamExample { public static void main(String args[]){ //creating IOTest object. IOTest obj = new IOTest(); //method call. obj.readFile(); } } |
Output:
www.w3schools.com |
ByteArrayOutputStream:
ByteArrayOutputStream class uses byte array to write data. The buffer automatically grows as data is written to it.
Commonly used to constructors of ByteArrayOutputStream:
1. ByteArrayOutputStream ()
Creates a new byte array output stream.
2. ByteArrayOutputStream (int size)
Creates a new byte array output stream, with a buffer capacity of the specified size, in bytes.
Commonly used methods of ByteArrayOutputStream:
1. public void writeTo(OutputStream outSt)
Writes the entire content of this Stream to the specified stream argument.
Example:
ByteArrayOutputStreamExample.java
import java.io.ByteArrayOutputStream; import java.io.FileOutputStream; /** * This program is used to write data into * a file using ByteArrayOutputStream. * @author w3schools */ class IOTest{ String str = "Hello www.w3schools.com"; public void writeFile(){ try { //Creating FileOutputStream object. //It will create a new file before writing if not exist. FileOutputStream fos = new FileOutputStream("F:\\New folder\\data5.txt"); //Creating ByteArrayOutputStream object. ByteArrayOutputStream baos=new ByteArrayOutputStream(); baos.write(str.getBytes()); baos.writeTo(fos); baos.flush(); //Close file after write operation. baos.close(); System.out.println("Contents written successfully."); } catch (Exception e) { e.printStackTrace(); } } } public class ByteArrayOutputStreamExample { public static void main(String args[]){ //Creating IOTest object. IOTest obj = new IOTest(); //method call obj.writeFile(); } } |
Output:
Download this example.
Next Topic: DataInputStream and DataOutputStream in java with example.
Previous Topic: FileInputStream and FileOutputStream in java with example.