Download Raw Binary Format To store numeric data in text files, each digit is

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Raw Binary Format
The way data is stored in memory is sometimes called the raw binary format.
int
We are used to writing numbers using the decimal or base 10 form which allows for 10 digits to represent numbers
– the digits 0-9.
1297 10 = (102 *1)+ (101 *9)+ (100 *2)
=100 + 90 + 2
However, in Java, integers are represented in memory using the binary numbering system, base 2 which allows for
only two digits to represent numbers (0-1)
12972 = (210 * 1)+ (29 * 0)+ (28 * 1)+ (27 * 0)+ (26 * 0)+ (25 * 0)+ (24 * 1)+ (23 * 0)+ (22 * 0)+ (21 * 0)+ (20 * 1)
= 1024 + 0 + 256 + 0 + 0 +
0 + 16 +
0 +
0 + 0
+ 1
FOUR (4) bytes are used to store integers so integer 1297 is (00000000 00000000 00000101 00010001)
char
Characters are internally represented by numbers. Each character is assigned a unique number. Java uses Unicode,
which is a set of numbers that are used as codes for representing characters. Each Unicode number requires two
bytes of memory, so char variables occupy two bytes.
When a character is stored in memory it is actually the numeric code that is stored. When the computer is
instructed to print the value on the screen, it displays the character that corresponds with the numeric code. For
example, the number 65 is the code for A, 66 is the code for B and so on. Notice the digits 0-9 have codes 48-57
//Characters correspond to numeric code
char letter;
letter = 65;
System.out.println(letter); //A
letter = 'A';
System.out.println(letter); //A
//Type Casting
System.out.println( (int) 'A'); //65
System.out.println( (char) 65); //A
The letter ‘A’ is stored internally in two bytes as the same as the binary representation of 65 (00000000 01000001).
Each character in the String “1297” is stored separately as the binary representation of ‘1’, ‘2’ ‘9’ and ‘7’. Therefore the
required memory is (00000000 01000001) (00000000 01000010) (00000000 01000111) (00000000 01001001).
To store numeric data in text files, each digit is converted into a character before storing it. When reading
data from the text file, methods are used to read each character and convert the digits into one integer. This
is time consuming.
Binary Files
Binary Files contain data stored in its raw binary format. Numerical data transfers faster and more compactly in a
raw binary format than as text characters.
Data in a binary file may not be viewed correctly in a text editor such as Notepad. When a text editor opens a file, it
assumes the file contains text. Therefore if the integer 65 was stored in the binary file, it would appear as an A on
the screen of a text editor.
Writing Binary Files
 Create objects from the following classes:
 FileOutputStream - allows you to open a file for writing binary data. It provides only basic
functionality for writing bytes to the file.
 ObjectOutputStream – allows you to write primitive data types and graphs of Java objects to a
binary file. It is used in conjunction with a FileOutputStream object that has a connection to a file.
 Use the methods found in ObjectOutputStream to write variables
 void writeDouble(double d)
 void writeInt( int i)
 void writeChar (int c)
 void writeUTF (String s) // Writes string object using Unicode Text Format
 void writeObject(Object o);
Reading Binary Files
 Create objects from the following classes:
 FileInputStream - allows you to open a file for reading binary data. It provides only basic
functionality for reading bytes from the file.
 ObjectInputStream – allows you to read primitive data types and graphs of Java objects from a
binary file. It is used in conjunction with a FileInputStream object that has a connection to a file.
 Use the methods found in ObjectInputStream to read variables
 double readDouble()
 int readInt( )
 char readChar ()
 String readUTF () // read String object using Unicode Text Format
 Object readObject(); //you must cast the return value to the desired class type.
More with writing Objects
An object’s fields can be retrieved and saved to a file in a record. If an object contains other types of objects as field,
however, the process of saving its contents can become complicated. Fortunately, Java allows you to serialize
objects which is a simpler way of saving objects to a file.
When an object is serialized it is converted into a series of bytes that contains the objects data. If the object is set up
properly, even the other objects that it might contain as fields are automatically serialized. The resulting set of bytes
can be saved to a file for later retrieval.
In order for an object to be serialized, its class must implement the Serializable interface located in the
jabv.io.package. It does not have any methods or fields but is used to let the Java complier know that objects might
be serialize. If a class contains objects of other classes as fields, those classes must also implement the Serializable
interface.
import java.util.*;
import java.io.*;
public class WriteBinaryExample
{
public static void main (String [] args) throws Exception
{
//Create Binary File for Writing
FileOutputStream fOut = new FileOutputStream("data.bin");
ObjectOutputStream outfile = new ObjectOutputStream(fOut);
//Write primitive values and Strings
outfile.writeChar('A');
outfile.writeInt(65);
outfile.writeUTF("hello");
//Write object
Employee emp = new Employee("George J. Jetson", 5.00, 10);
outfile.writeObject(emp);
outfile.close();
//Open Binary File for Reading
FileInputStream fIn = new FileInputStream("data.bin");
ObjectInputStream infile = new ObjectInputStream(fIn);
char ch;
int num;
String s;
Employee tempEmp;
ch = infile.readChar();
num = infile.readInt();
s = infile.readUTF();
tempEmp = (Employee) infile.readObject(); //type cast
System.out.println("ch:" + ch);
System.out.println("num: " + num);
System.out.println("s: " + s);
System.out.println("Employee: " + tempEmp + " -- " + tempEmp.getPay());
infile.close();
}
}
//----------------------------------------------------------------------------------------------------------------------------- ---------class Employee implements Serializable
{ private String name;
private double rate;
private double hours;
public Employee(String name, double r, double h)
{
this.name = name;
this.rate = r;
this.hours = h;
}
public double getPay()
{
return rate * hours;
}
public String toString()
{
return name + " " + rate + " " + hours;
}
}