Java Write To File Binary

In this tutorial you will learn how to write to binary file.

Java Write To File Binary

In this tutorial you will learn how to write to binary file.

Java Write To File Binary

Java Write To File Binary

In this tutorial you will learn how to write to binary file.

A binary file is a file into which the bit patterns of mostly data types can be formed to a byte of 8 bits. Write to a binary file in java in the example given below I have created some data arrays that contains some values. A binary FileOutputStream is opened for the file and wrapped this stream object to BufferedOutputStream instance and then finally wrapped this stream object into the instance of DataOutputStream class. This (DataOutputStream) class has the method writeXXXX() that provides the facility of writing primitive types. Here XXXX indicates the primitive types e.g. writeByte(int i), writeFloat(float f), writeUTF(String str) etc.

writeByte(int i) : Method of the DataOutputStream class writes a byte in the output stream.

writeFloat(float f) : Method of the DataOutputStream class first converts the argument to an 'int' and then writes the converted value in the output stream as a 4 byte quantity.

Methods that I uses in the example are as follows :

writeUTF(String str) : Method of the DataOutputStream class that writes the string to the output stream.

writeInt(int i) : Method of the DataOutputStream class that writes the integer value to the output stream.

writeDouble(double d) : Method of DataOutputStream class first converts the argument to a long and then writes the converted value in the output stream as a 8 byte quantity.

Example :

import java.io.*;

class WriteToFileBinary
 {
  public static void main(String args[])
   {
    WriteToFileBinary wtfb = new WriteToFileBinary();
    wtfb.writeToFileBinary();
   }
   public void writeToFileBinary()
    {
     String [] str ={ "A", "B", "C", "D"};
     int[] in = { 12, 8, 13, 29, 50 };
     double[] dob = { 19.99, 9.99, 15.99, 3.99, 4.99 };
     FileOutputStream fos = null;
     BufferedOutputStream bos = null;
     DataOutputStream dos = null;
     try
      {
       fos = new FileOutputStream("writeToFileBinary");
       bos = new BufferedOutputStream(fos);
       dos = new DataOutputStream(bos);
       for (int i = 0; i < str.length; i ++)
        {
         dos.writeDouble(dob[i]);
         dos.writeInt(in[i]);
         dos.writeUTF(str[i]);
        }
	dos.close();
      }
      catch(Exception e)
       {
        System.out.println(e);
       }          
     }
  }

How to Execute this example :

After doing the basic process to execute a java program write simply on command prompt as :

javac WriteToFileBinary.java to compile the program

And after successfully compilation to run simply type as :

java WriteToFileBinary

When you will execute this example a new file will be created at the specified place given by you with containing the text that you are trying to write into the file by java program.

Download Source Code