mydomain
No ADS
No ADS

Java IO Binary Streams Tutorial with Examples

  1. Overview of binary stream
  2. Class InputStream & OutputStream
  3. Class ByteArrayInputStream & ByteArrayOutputStream
  4. Class ObjectInputStream & ObjectOutputStream
  5. Class DataInputStream & DataOutputStream
  6. SequenceInputStream
  7. PipedInputStream & PipedOutputStream
  8. Class PrintStream

1. Overview of binary stream

Binary Stream is led by two classes: InputStream and OutputStream. Following two classes is a variety of affiliated classes. As for the balance of power, the relationship of binary stream is more diverse and sophisticated than that of character stream. Regarding JDK1.5 in binary stream, two classes, LineNumberInputStream and StringBufferInputStream, are advised not to use because they are deprecated.

2. Class InputStream & OutputStream

No ADS
InputStream Class is an abstract class, so you can't create the InputStream object by the InputStream class itself. However, this class divides into many branches. In specific situations, you can create InputStream object from constructors of derived classes.
// Java.io.InputStream is an abstract class
// Could not initialize InputStream object directly, through class InputStream.
// Initialize InputStream object should be through its subclasses ..

InputStream fileStream =new FileInputStream("C:/test.txt");

// Input stream from the keyboard ..
InputStream is = System.in;
OutputStream Class is a abstract class so you cannot create OutputStream object through OutputStream class. However, this class separates into branches with subclasses that play an important role. In some cases, you can create InputStream object from constructors of subclasses.
// Java.io.OutputStream is an abstract class
// Could not initialize OutputStream object directly, through class OutputStream.
// Initialize OutputStream object should be through its subclasses ..

// Stream write to file.
OutputStream os=new FileOutputStream("D:/outData.txt");

// Stream write to console.
OutputStream w=System.out;
HelloInputStream.java
package org.o7planning.tutorial.javaio.stream;

import java.io.FileInputStream;
import java.io.InputStream;

public class HelloInputStream {

    public static void main(String[] args) {
        try {
       
            // Create InputStream object from subclass.
            // This is Stream read file.
            InputStream is = new FileInputStream("data.txt");

            int i = -1;
           
            // Read the turn of bytes in the stream.
            // Each time the 8-bit read, convert it to int.
            // Read the value of -1 means the end of the stream.
            while ((i = is.read()) != -1) {
                System.out.println(i + "  " + (char) i);
            }
            is.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
Result:
HelloOutputStream.java
package org.o7planning.tutorial.javaio.stream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class HelloOutputStream {

   public static void main(String[] args) {
       try {
           File dir = new File("C:/Test");

           // Create directories if not exists
           dir.mkdirs();

           // Create output Stream write data to file.
           OutputStream w = new FileOutputStream(
                   "C:/Test/test_outputStream.txt");
           
           // Create array of bytes, write it to stream.
           byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o' };

           // write turn the bytes into the stream
           for (int i = 0; i < by.length; i++) {
               byte b = by[i];

               // Write 1 byte.
               w.write(b);
           }
           // Close the output stream, finish write file.
           w.close();
       } catch (Exception e) {
           e.printStackTrace();
       }
   }
}
Result:
Two above examples are simple. They read or write each byte. In the next example, we read or write simultaneously various bytes. This helps increase the processing speed.
InputStreamExample2.java
package org.o7planning.tutorial.javaio.stream;

import java.io.FileInputStream;
import java.io.InputStream;

public class InputStreamExample2 {

    public static void main(String[] args) {
        try {

            // Create input stream, read a file.
            InputStream in = new FileInputStream("data.txt");

            // A temporary array to store data each reading
            byte[] temp = new byte[10];
            int i = -1;

           
            // Reads some number of bytes from the input stream
            // and stores them into the buffer array 'temp'.
            // Return the number of bytes actually read.
            // return -1 if end of stream.
            while ((i = in.read(temp)) != -1) {

                // Create String from bytes
                String s = new String(temp, 0, i);
                System.out.println(s);
            }
            in.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}
These are illustrative images for reading simultaneously in the above example:
OutputStreamExample2.java
package org.o7planning.tutorial.javaio.stream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStream;

public class OutputStreamExample2 {

    public static void main(String[] args) {
        try {
            File dir = new File("C:/Test");

            // Create directories if not exists.
            dir.mkdirs();            
            
            // Create output Stream to write file.
            OutputStream os = new FileOutputStream("C:/Test/test_writerOutputStream.txt");
            
            // Create array of bytes, write bytes into the file above.
            byte[] by = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 31, 34, 92 };
            byte[] by2 = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'b', 'o',
                    'y' };
            
            // Write all of bytes in array into Stream.
            os.write(by);
            
            // Flush data in memory to file.
            os.flush();
            
            // Continue write the 2nd byte array to the stream
            os.write(by2);
            
            // Close the output stream, finish write file.
            os.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
}

3. Class ByteArrayInputStream & ByteArrayOutputStream

No ADS
ByteArrayInputStream wrap a Array of bytes (byte[] buf) and through ByteArrayInputStream to access elements of Array.
ByteArrayOutputStream is a stream of bytes, in which it contains a array of bytes (byte[]buf) that are able to increase the size by themselves when the number of bytes in stream rises. Each time the bytes is written to the stream, it also means assigning those bytes to array

When the array is full of assigned elements, the program creates a new longer array and copies elements of old array to... (It is its own increase in size of the bytes array as above mentioned)

Some methods of ByteArrayOutputStream:
// return the current contents of this output stream, as a byte array.
  - byte[] toByteArray();

// String decoded from the buffer's contents.
  - String toString() ;

// return the number of valid bytes in this output stream.
  - int size();
ByteArrayInputStreamExample.java
package org.o7planning.tutorial.javaio.bytestream;

import java.io.ByteArrayInputStream;
import java.io.IOException;

public class ByteArrayInputStreamExample {

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

        // Byte Array.
        byte[] bytes = new byte[] { 'H', 'e', 'l', 'l', 'o', ' ', 'I', 'O' };

        // Using ByteArrayInputStream to read bytes array.
        ByteArrayInputStream bInput = new ByteArrayInputStream(bytes);

        System.out.println("Converting characters to Upper case ");
        int c = 0;

        // Read the turn of bytes in the stream.
        // Cursor will move from the beginning to the end of the array array.
        // Every time you read a byte pointer will move one step to the end.        
        while ((c = bInput.read()) != -1) {
            char ch = (char) c;
            ch = Character.toUpperCase(ch);
            System.out.println(ch);
        }

        // Check whether this stream supports mark or not
        boolean markSupport = bInput.markSupported();

        System.out.println("Mark Support? " + markSupport);
       
        // Move the cursor to the default location
        // In this example, it will move to position 0 ..        
        bInput.reset();

        char ch = (char) bInput.read();
        System.out.println(ch);

         // Read next byte
        ch = (char) bInput.read();
        System.out.println(ch);

        System.out.println("Skip 4");
 
        // Skip 4 bytes
        bInput.skip(4);
        ch = (char) bInput.read();
        System.out.println(ch);

    }

}
Result:
ByteArrayOutputStreamExample.java
package org.o7planning.tutorial.javaio.bytestream;

import java.io.ByteArrayOutputStream;
import java.io.IOException;

public class ByteArrayOutputStreamExample {

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


        // Create ByteArrayOutputStream object.
        // Object contains within it an array of bytes.
        // Array with size 12 elements.
        // If the number of elements to write to stream more than 12, the array will be replaced by
        // new array has more elements, and copy the elements of old array into.        
        ByteArrayOutputStream bOutput = new ByteArrayOutputStream(12);

        String s = "Hello ByteArrayOutputStream";

        for (int i = 0; i < s.length(); i++) {

            char ch = s.charAt(i);

            if (ch != 'a' && ch != 'e') {
                bOutput.write(ch);
            }
        }

        // Returns the current size of the buffer.
        int size = bOutput.size();
        System.out.println("Size = " + size);

        byte[] bytes = bOutput.toByteArray();

        String ss = new String(bytes);

        System.out.println("New String = " + ss);
    }
}
Kết quả:

4. Class ObjectInputStream & ObjectOutputStream

No ADS
ObjectInputStream and ObjectOutputStream allow you to read or write an Object in the stream. These objects must be Serializable type (It means they can be arranged in queue).
Here are some examples:
Student.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.Serializable;

public class Student implements Serializable {

   private static final long serialVersionUID = -5074534753977873204L;

   private String firstName;

   private String lastName;

   public Student(String firstName, String lastName) {
       this.firstName = firstName;
       this.lastName = lastName;
   }

   public String getFirstName() {
       return firstName;
   }

   public void setFirstName(String firstName) {
       this.firstName = firstName;
   }

   public String getLastName() {
       return lastName;
   }

   public void setLastName(String lastName) {
       this.lastName = lastName;
   }

}
Pupil.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.Serializable;

public class Pupil implements Serializable {
 
   private static final long serialVersionUID = -8501383434011302991L;
   
   private String fullName;

   public Pupil(String fullName)  {
       this.fullName= fullName;
   }
   
   public String getFullName() {
       return fullName;
   }

   public void setFullName(String fullName) {
       this.fullName = fullName;
   }

}
ObjectOutputStreamExample.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.Date;

public class ObjectOutputStreamExample {

    public static void main(String[] args) throws IOException {
        File dir = new File("C:/Test");

        // Create directories if not exists.
        dir.mkdirs();


        // Create stream write to file.
        FileOutputStream fos = new FileOutputStream(
                "C:/Test/testObjectStream.txt");

       
        // Create ObjectOutputStream object wrap 'fos'.
        // Data written to this stream will be pushed to 'fos'.
        ObjectOutputStream oos = new ObjectOutputStream(fos);
        
        
        // Write String to Stream.
        oos.writeUTF("This is student, pupil profiles");
    
  
        // Note: Write Serializable object only.
        // Write an Object to stream.
        oos.writeObject(new Date());

        Student student1 = new Student("Thanh", "Phan");
        Student student2 = new Student("Ngan", "Tran");
        Pupil pupil1 = new Pupil("Nguyen Van Ba");

        oos.writeObject(student1);
        oos.writeObject(pupil1);
        oos.writeObject(student2);

        oos.close();
        System.out.println("Write successful");
    }

}
Results of running:
This is an illustrated image of writing Object on file. It is processed in turn. In the future, when reading, you have to remember the order you write so that you can read it right.
And this is an example of the ObjectInputStream of reading file as mentioned in the above example:
ObjectInputStreamExample.java
package org.o7planning.tutorial.javaio.objstream;

import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.Date;

public class ObjectInputStreamExample {

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

        // Create stream to read file.
        FileInputStream fis = new FileInputStream(
                "C:/Test/testObjectStream.txt");

        // Create ObjectInputStream object wrap 'fis'.
        ObjectInputStream ois = new ObjectInputStream(fis);

        // Read String.
        String s = ois.readUTF();

        System.out.println(s);

        // Read Object.
        Date date = (Date) ois.readObject();
        System.out.println("Date = " + date);

        Student student1 = (Student) ois.readObject();

        System.out.println("Student " + student1.getFirstName());

        Pupil pupil = (Pupil) ois.readObject();

        System.out.println("Pupil " + pupil.getFullName());

        Student student2 = (Student) ois.readObject();

        System.out.println("Student " + student2.getFirstName());

        ois.close();
    }
}
Result:

5. Class DataInputStream & DataOutputStream

No ADS
DataOutputStream **
// Constructor
public DataOutputStream(OutputStream out)

// Write a character 16 bit (2-byte)
public void writeChar(int val)
// Write double 64 bit (8-byte)
public void writeDouble(double val)
// Write float 32 bit (4-byte)
public void writeFloat(float val)
// Write integer 32 bit (4-byte)
public void writeInt(int val)
// Write String UTF-8.
public void writeUTF(String obj)
....
DataInputStream **
// Constructor
public DataInputStream(InputStream in)

// Read a character 16 bit (2 byte)
public char readChar()
// Read double 64 bit (8 byte)
public double readDouble()
// Read float 32 bit (4 byte)
public float readFloat()
// Read int 16 bit (4 byte)
public int readInt()
// Read UTF-8 String.
public String readUTF()
....
DataOutputStreamExample.java
package org.o7planning.tutorial.javaio.datastream;

import java.io.DataOutputStream;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;

public class DataOutputStreamExample {

    public static void main(String[] args) throws IOException {
        int cityIdA = 1;
        String cityNameA = "Green Lake City";
        int cityPopulationA = 500000;
        float cityTempA = 15.50f;

        int cityIdB = 2;
        String cityNameB = "Salt Lake City";
        int cityPopulationB = 250000;
        float cityTempB = 10.45f;

        File dir = new File("C:/Test");
        dir.mkdirs();

        //
        // Create FileOutputStream write to file.
        //
        FileOutputStream fos = new FileOutputStream("C:/Test/cities.txt");

        // Create DataOutputStream object wrap 'fos'.
        // The data write to 'dos' will be pushed to 'fos'.
        DataOutputStream dos = new DataOutputStream(fos);

        //
        // Write data.
        //
        dos.writeInt(cityIdA);
        dos.writeUTF(cityNameA);
        dos.writeInt(cityPopulationA);
        dos.writeFloat(cityTempA);

        dos.writeInt(cityIdB);
        dos.writeUTF(cityNameB);
        dos.writeInt(cityPopulationB);
        dos.writeFloat(cityTempB);

        dos.flush();
        dos.close();
    }

}
Running DataOutputStreamExample class and receive a data file is written out.
DataInputStreamExample.java
package org.o7planning.tutorial.javaio.datastream;

import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;

public class DataInputStreamExample {
    
    
    
    public static void main(String[] args) throws IOException {
        
         // Stream to read file.
         FileInputStream fis = new FileInputStream("C:/Test/cities.txt");

         // Create DataInputStream object wrap 'fis'.
         DataInputStream dis = new DataInputStream(fis);

         //
         // Read data.
         //
         int cityId1 = dis.readInt();
         System.out.println("Id: " + cityId1);
         String cityName1 = dis.readUTF();
         System.out.println("Name: " + cityName1);
         int cityPopulation1 = dis.readInt();
         System.out.println("Population: " + cityPopulation1);
         float cityTemperature1 = dis.readFloat();
         System.out.println("Temperature: " + cityTemperature1);

         //
         // Read data.
         //
         int cityId2 = dis.readInt();
         System.out.println("Id: " + cityId2);
         String cityName2 = dis.readUTF();
         System.out.println("Name: " + cityName2);
         int cityPopulation2 = dis.readInt();
         System.out.println("Population: " + cityPopulation2);
         float cityTemperature2 = dis.readFloat();
         System.out.println("Temperature: " + cityTemperature2);
         
         dis.close();
    }
}
Results:

6. SequenceInputStream

Now you have been familiar with reading a file and getting a input stream. However, in fact, you sometimes need to read many files and join the data for writing another file, for example. It means you join many input streams together for creating a larger stream composed of smaller streams in tandem. We are talking about java.io.SequenceInputStream class. There is no equivalent definition to the output stream...
// Constructor

 // Create new Stream from Pairing two streams together
// which will be read in order
public SequenceInputStream(InputStream s1,InputStream s2)  
 
// Create new Stream from Multi input stream.
// which will be read in order
  public SequenceInputStream(Enumeration<? extends InputStream> e)
Example:
// Input stream read a file - File1.txt .
InputStream is1=new FileInputStream("File1.txt");
// Input stram read a file - File2.txt  
InputStream is2=new FileInputStream("File2.txt");

// Create new Stream from two stream
SequenceInputStream sis=new SequenceInputStream(is1,is2);

7. PipedInputStream & PipedOutputStream

No ADS
Suppose you have two streams: an input and an output. The input of A data reads a file and get the information for writing on the B data stream whose output is another file. A and B streams is totally different in this case... Thus, in the application, you have to have three actions:
  • Create stream read a file - A
  • Create stream B writing to file.
  • Read data from A to write to B...
The two first actions is compulsory, but if you want to get rid of the third action... It means we need to create an implicit connection between the input and output stream, so that once data appear on the A reading head they can be recognized and read by the B output stream... This is called an piped connection between input and output threads.
PipeStreamExample1.java
package org.o7planning.tutorial.javaio.pipestream;

import java.io.IOException;
import java.io.InputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;

public class PipeStreamExample1 {

	private InputStream pipedInputStream;

	public static void main(String[] args) throws IOException, InterruptedException {
		new PipeStreamExample1().test();
	}

	private void test() throws IOException, InterruptedException {
		// Create a 'pipedOutputStream',
		PipedOutputStream pipedOutputStream = new PipedOutputStream();

		// Data writing to 'pipedOutputStream'
		// will automatically appear in 'pipedInputStream'.
		pipedInputStream = new PipedInputStream(pipedOutputStream);

		new ThreadRead().start();

		char[] chs = new char[] { 'a', 'a', 'b', 'c' , 'e' };

		// Write data to 'pipedOutputStream'.
		for (char ch : chs) {
			pipedOutputStream.write(ch);
			Thread.sleep(1000);
		}
		pipedOutputStream.close();
	}

	// A Thread to read the data that appears on 'pipedInputStream'.
	class ThreadRead extends Thread {

		@Override
		public void run() {
			try {
				int data = 0;
				while ((data = pipedInputStream.read()) != -1) {
					System.out.println((char) data);
				}
			} catch (Exception e) {
				e.printStackTrace();
			} finally {
				closeQuietly(pipedInputStream);
			}
		}
	}

	private void closeQuietly(InputStream is) {
		if (is != null) {
			try {
				is.close();
			} catch (IOException e) {
			}
		}
	}

}

8. Class PrintStream

No ADS

// PrintStream is the subclass of FilterOutputStream.
// It can wrap a binary output stream (OutputStream) , ..
// Constructor :

   // Wrap a OutputStream
   public PrintStream(OutputStream out)

   public PrintStream(OutputStream out,boolean autoFlush)

   // Write to file ..
   public PrintStream(String fileName)
 

// Some methods
   public void println(String s)
   public void print(char ch)

   // Write an Object
   public void print(Object obj)

   // Write long value (64bit)
   public void print(long n)

   public PrintStream append(java.lang.CharSequence csq) .
   // ... (more see javadoc)
You already know to catch exceptions through try-catch.
try {    
 // Do something here
 // Error divided by 0
 int i=10/0;
}
// The catch block is executed
catch(Exception e) {  
 // Print out message
 System.out.println("Error on try..."+e.getMessage());
 // Print 'stack trace' to Console.
 // How to get the text "stack trace"?
 e.printStackTrace();
}
This is the 'Stack Trace' you often see when something error.
The following example retrieves String "Stack Trace"
GetStackTraceString.java
package org.o7planning.tutorial.javaio.printstream;

import java.io.ByteArrayOutputStream;
import java.io.PrintStream;

public class GetStackTraceString {

  private static String getStackTraceString(Exception e) {
 
      // Create ByteArrayOutputStream
      ByteArrayOutputStream baos = new ByteArrayOutputStream();


      // Data written to this stream will be pushed to 'baos'.
      PrintStream printStream = new PrintStream(baos);


      // Prints this throwable and its backtrace to 'printStream'
      e.printStackTrace(printStream);

      printStream.close();
     
      byte[] bytes = baos.toByteArray();

      String s = new String(bytes);
      return s;
  }

  public static void main(String[] args) {

      try {
          // Do something here
          // Error divided by 0
          int i = 10 / 0;
      }
      // The catch block is executed
      catch (Exception e) {
          // Print out message
          System.out.println("Error on try..." + e.getMessage());

          // Get the text "stack trace"
          String s = getStackTraceString(e);

          System.out.println("Stack Trace String " + s);
      }
  }
}
The code below print 'stack trace' to the console, but using method printStackTrace(PrintStream) instead of using the method printStackTrace() which defaults to print 'stack trace' to the console.
// PrintStream to write to the Console
PrintStream os = System.out;
// Exception e ..
// Write 'stack trace' to 'os'. This means print to the Console.
e.printStackTrace(os);

// In essence it is equivalent to calling:
e.printStackTrace();
No ADS

Java IO Tutorials

Show More
No ADS