Monday, August 15, 2016

How to process binary data

Sometimes, in real world cases, you might have to deal with binary data which is a little bit different in processing from text based data. The examples bellow show you how to simply deal with this type of flows.

Example 1:

import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

// A simple class to illustrate data streams; write constants 0 and 1 in different 
// data type values into a file and read the results back and print them 
class DataStreamExample {
public static void main(String[] args) {
// write some data into a data file with hard-coded name "temp.data"
try (DataOutputStream dos = new DataOutputStream(new FileOutputStream("temp.data"))) {
// write values 1 to 10 as byte, short, int, long, float and double
// omitting boolean type because an int value cannot
// be converted to boolean
for (int i = 0; i < 10; i++) {
dos.writeByte(i);
dos.writeShort(i);
dos.writeInt(i);
dos.writeLong(i);
dos.writeFloat(i);
dos.writeDouble(i);
}
} catch (FileNotFoundException fnfe) {
System.err.println("cannot create a file with the given file name ");
System.exit(-1); // don’t proceed – exit the program
} catch (IOException ioe) {
System.err.println("an I/O error occurred while processing the file");
System.exit(-1); // don’t proceed – exit the program
}
// the DataOutputStream will auto-close, so don't have to worry about it
// now, read the written data and print it to console
try (DataInputStream dis = new DataInputStream(new FileInputStream("temp.data"))) {
// the order of values to read is byte, short, int, long, float and
// double since we've written from 0 to 10,
// the for loop has to run 10 times
for (int i = 0; i < 10; i++) {
// %d is for printing byte, short, int or long
// %f, %g, or %e is for printing float or double
// %n is for printing newline
System.out.printf("%d %d %d %d %g %g %n", dis.readByte(), dis.readShort(), dis.readInt(),
dis.readLong(), dis.readFloat(), dis.readDouble());
}
} catch (FileNotFoundException fnfe) {
System.err.println("cannot create a file with the given file name ");
} catch (IOException ioe) {
System.err.println("an I/O error occurred while processing the file");
} // the DataOutputStream will auto-close, so don't have to worry about
// it
}
}

Outputs:

0 0 0 0 0.00000 0.00000 
1 1 1 1 1.00000 1.00000 
2 2 2 2 2.00000 2.00000 
3 3 3 3 3.00000 3.00000 
4 4 4 4 4.00000 4.00000 
5 5 5 5 5.00000 5.00000 
6 6 6 6 6.00000 6.00000 
7 7 7 7 7.00000 7.00000 
8 8 8 8 8.00000 8.00000 
9 9 9 9 9.00000 9.00000 

Example 2:

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.HashMap;
import java.util.Map;

// A simple class to illustrate object streams: fill a data structure, write it to a 
// temporary file and read it back and print the read data structure 
class ObjectStreamExample {
public static void main(String[] args) {
Map<String, String> presidentsOfUS = new HashMap<>();
presidentsOfUS.put("Barack Obama", "2009 to --, Democratic Party, 56th term");
presidentsOfUS.put("George W. Bush", "2001 to 2009, Republican Party, 54th and 55th terms");
presidentsOfUS.put("Bill Clinton", "1993 to 2001, Democratic Party, 52nd and 53rd terms");
try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("object.data"))) {

oos.writeObject(presidentsOfUS);
} catch (FileNotFoundException fnfe) {
System.err.println("cannot create a file with the given file name ");
} catch (IOException ioe) {
System.err.println("an I/O error occurred while processing the file");
} // the ObjectOutputStream will auto-close, so don't have to worry
// about it

try (ObjectInputStream ois = new ObjectInputStream(new FileInputStream("object.data"))) {

Object obj = ois.readObject();
// first check if obj is of type Map
if (obj != null && obj instanceof Map) {
Map<?, ?> presidents = (Map<?, ?>) obj;
System.out.println("President name \t Description");
for (Map.Entry<?, ?> president : presidents.entrySet()) {
System.out.printf("%s \t %s %n", president.getKey(), president.getValue());
}
}
} catch (FileNotFoundException fnfe) {
System.err.println("cannot create a file with the given file name ");
} catch (IOException ioe) {
System.err.println("an I/O error occurred while processing the file");
} catch (ClassNotFoundException cnfe) {
System.err.println("cannot recognize the class of the object - is the file corrupted?");
}
}
}

Outputs:

President name Description
Barack Obama 2009 to --, Democratic Party, 56th term 
George W. Bush 2001 to 2009, Republican Party, 54th and 55th terms 
Bill Clinton 1993 to 2001, Democratic Party, 52nd and 53rd terms

No comments:

Post a Comment