Page 1 of 1

Writing information to a binary file Rate Topic: -----

#1 calebjonasson  Icon User is offline

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

Reputation: 209
  • View blog
  • Posts: 989
  • Joined: 28-February 09

Posted 07 April 2010 - 08:20 PM

The first thing we need to do is import our classes. The classes we will import are going to be the following: “ObjectOutputStream”, “FileOutputStream” and the IOException.

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


The next thing that is required is to create the object “ObjectOutputStream.” We will do this by using the classes we imported at the start of this tutorial. We will also be placing this into a try catch statement to see if we are properly opening the file.

String fileName = "file.dat";
try{
    outputStream = new ObjectOutputStream(new FileOutputStream(fileName));
}catch(IOException e){
    System.out.println("Could not open the file." + e);
    System.exit(0);
}


Take note that we are getting the file name from the a string variable and using it in our new file stream.

Since we now have our file created and classes imported the next thing required is for us to create all of the variables we will be using in this example. Since I will be using all of the primitive variable types there will be a fair sized list you will need to type out.

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;


The next step is to write all of the variables to the file through the output stream. Once again this will be done using try catch block but you shouldn’t have a problem if you follow along correctly.

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);
}


The final piece of the code will close the output stream and tell us that we have successfully written to a file.

System.out.println("Records have been written to the file.");


The file with everything in it is as follows:

import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
public class writebin {
    public static void main(String[] args) {
        String fileName = "file.dat";
 
        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.");
    }
 
}

Attached File  write_information_to_binary_java.zip (990bytes)
Number of downloads: 154

Is This A Good Question/Topic? 0
  • +

Page 1 of 1