Page 1 of 1

Reading information from a binary file with java Rate Topic: -----

#1 calebjonasson  Icon User is offline

  • $bert = new DragonUnicorn(); $bert->rawr();
  • member icon

Reputation: 207
  • View blog
  • Posts: 988
  • Joined: 28-February 09

Posted 16 June 2010 - 09:12 AM

This tutorial is an extension from “writing information to a binary file with java.” It is in your best interest to read the previous article since this one will be continuing the code.

Previously we created a bunch of primitive data types along with a string. These files have been written to the data file entitled “file.dat” If you did not want to follow the previous article you can download the article here along with the data file within it.

We are going to want to add more classes to import. The classes that we need to import are the “ObjectInputStream” and “FileInputStream.” You should now have the following classes imported into your file.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;


Since we have already written our data to the file and closed it we will need to open the file to read it using the input stream. Like in the last tutorial we are going to need to associate the memory to blank then try to load the file that we created earlier. The code will look something like this.

ObjectInputStream inputStream = null;
        try{
            inputStream = new ObjectInputStream(new FileInputStream(fileName));
        }catch(IOException e){
            System.out.println("There was a problem opening the file: " + e);
            System.exit(0);
        }


The next thing to do is read the binary in the same order that it was written, top to bottom, so if we first write an integer then boolean we need to read an integer then a boolean or else we are going to get some wrong numbers coming back at us. The next part of the code is also in try catch using the Exception e.

Please note that it is good practice to place individual try catch whenever you read from the file and load into memory. This will allow you to better find errors but for the sake of this article we are going to save the space.

	

try{
            byte var1 = inputStream.readByte();
            short var2 = inputStream.readShort();
            int var3 = inputStream.readInt();
            long var4 = inputStream.readLong();
 
            float var5 = inputStream.readFloat();
            double var6 = inputStream.readDouble();
 
            char var7 = inputStream.readChar();
            String var8 = inputStream.readUTF();
 
            boolean b1 = inputStream.readBoolean();
 
            inputStream.close();
        }catch(Exception e){
            System.out.println("There was an issue reading from the file: " + e);
            System.exit(0);
        }


If you would like to take a look at all of the code used in this demo it is placed below for your convenience.

	

/* Code with Design
 *
 * Created By: Caleb Jonasson
 *
 * Desc: This class file is used to create a binary file,
 * write values to the file and check for errors along the way.
 */
 
import java.io.FileOutputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.ObjectInputStream;
 
public class writebin {
 
    public static void main(String[] args) {
        //the filename to create.
        String fileName = "file.dat";
 
        //creating the space in memory for the file
        ObjectOutputStream  outputStream = null;
 
        try{
            outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
        }catch(IOException e){
            System.out.println("Could not open the file." + e);
            System.exit(0);
        }
 
        byte foo = 1;
        short bar = 5;
        int baz = 543;
        long qurk = 123923892;
 
        float flt = 1;
        double dbl = 1.2323;
 
        char chr = 'a';
        String str = "hello";
 
        boolean bool = false;
 
        try{
            //integet based
            outputStream.writeByte(foo);
            outputStream.writeShort(bar);
            outputStream.writeInt(baz);
            outputStream.writeLong(qurk);
 
            //decimal based
            outputStream.writeFloat(flt);
            outputStream.writeDouble(dbl);
 
            //alpha based
            outputStream.writeChar(chr);
            outputStream.writeUTF(str);
 
            //true false based
            outputStream.writeBoolean(bool);
 
            outputStream.close();
 
        }catch(IOException e){
            System.out.println("Writing error: " + e);
            System.exit(0);
        }
        System.out.println("Records have been written to the file.");
 
        ObjectInputStream inputStream = null;
        try{
            inputStream = new ObjectInputStream(new FileInputStream(fileName));
        }catch(IOException e){
            System.out.println("There was a problem opening the file: " + e);
            System.exit(0);
        }
        try{
            byte var1 = inputStream.readByte();
            short var2 = inputStream.readShort();
            int var3 = inputStream.readInt();
            long var4 = inputStream.readLong();
 
            float var5 = inputStream.readFloat();
            double var6 = inputStream.readDouble();
 
            char var7 = inputStream.readChar();
            String var8 = inputStream.readUTF();
 
            boolean b1 = inputStream.readBoolean();
 
            inputStream.close();
        }catch(Exception e){
            System.out.println("There was an issue reading from the file: " + e);
            System.exit(0);
        }
        System.out.println("Reading of the file: " + fileName + " has been completed.");
 
    }
 
}


Is This A Good Question/Topic? 1
  • +

Page 1 of 1