0% found this document useful (0 votes)
51 views

File Access: Reading: Savitch, Chapter 9

This document discusses reading and writing binary files and objects in Java. It covers: 1) Defining DataInputStream/DataOutputStream objects to read from and write to binary files and using methods like readChar() and writeInt() to access the file. 2) Objects must implement Serializable to be written to files and ObjectInputStream/ObjectOutputStream are used with readObject()/writeObject() to serialize objects. 3) Recursively writing objects means that any nested objects are also written to the file and recovered during reading.

Uploaded by

mhòa_43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
51 views

File Access: Reading: Savitch, Chapter 9

This document discusses reading and writing binary files and objects in Java. It covers: 1) Defining DataInputStream/DataOutputStream objects to read from and write to binary files and using methods like readChar() and writeInt() to access the file. 2) Objects must implement Serializable to be written to files and ObjectInputStream/ObjectOutputStream are used with readObject()/writeObject() to serialize objects. 3) Recursively writing objects means that any nested objects are also written to the file and recovered during reading.

Uploaded by

mhòa_43
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 21

14/1

File Access
Reading: Savitch, Chapter 9
14/2
Objectives
To learn binary file accessing
To learn how to read/write object by object
14/3
Binary File Accessing
Binary file accessing involves
the following steps

(1) Define a
DataInputStream/DataOutputStream object
for reading/writing

14/4

Example

DataInputStream in = new DataInputStream
(new FileInputStream ("myBinaryFile"));

FileOutputStream outTemp = new
FileOutputStream ("myBinaryFile");
DataOutputStream out = new
DataOutputStream(outTemp);
14/5
(2) Use readChar(), readInt(), readFloat() ... to
read from the binary file.

Use writeChar(), writeInt(), writeFloat(),
writeChars() ... to write to the binary file.

Example
char c = in.readChar();
out.writeChars(What a wonderful world!);
14/6
(3) close the file


14/7
Example

class NewBorn {
private float weight;
private char gender;
NewBorn(float _w, char _g)
{weight = _w; gender = _g;}
public float getWeight() {return weight;}
public char getGender() {return gender;}
}
14/8
//BinaryFileTest.java
//The program writes each newborns details
//into a binary file.
import java.io.*;

public class BinaryFileTest
{
public static void main(String[] args) throws IOException {
final int N = 2;
NewBorn nbList[ ] = new NewBorn[N];
nbList[0] = new NewBorn(3.4F, 'F');
nbList[1] = new NewBorn(4.2F, 'M');
14/9

FileOutputStream outTemp = new
FileOutputStream ("myBinaryFile");
DataOutputStream out = new
DataOutputStream(outTemp);

for (int i = 0; i < N; i++) {
out.writeFloat(nbList[i].getWeight());
out.writeChar(nbList[i].getGender());
}

out.close();
14/10
DataInputStream in = new DataInputStream
(new FileInputStream ("myBinaryFile"));

for (int i = 0; i < N; i++) {
System.out.print(in.readFloat());
System.out.println(in.readChar());
}
}
}
14/11
Object Serialization
ObjectInputStream and ObjectOutputStream
are two classes in java.io. They can be used
to read/write objects.



14/12
Reading/Writing object by object involves
the following steps

(1) The class, from which its object will be
read/written, implements the interface
Serializable.
Serializable is an interface defined in java.io.


14/13
Example

class NewBorn implements Serializable {
... ...
}
14/14
(2) Define
ObjectInputStream/ObjectOutputStream
object for reading/writing.

Example
ObjectOutputStream out = new
ObjectOutputStream(new
FileOutputStream ("myObjectFile"));

14/15
(3) Use readObject()/writeObject() methods to
read/write.

Example

NewBorn myChild = (NewBorn) in.readObject();
14/16
(4) close the file.

14/17
Note
When a field in an object refers to another
object, the writeObject() method is invoked
recursively to serialize that object (the field)
as well.

Similarly, readObject() method recovers such
an object recursively.
14/18
Example

//TriangleFile.java
//the program writes and reads Triangle objects.
import java.io.*;
class Point implements Serializable {
double x, y;
Point(double _x, double _y) {x = _x; y = _y;}
public String toString() { return "(" + x + ", " + y + ") "; }
}

14/19
class Triangle implements Serializable {
Point a, b, c;
Triangle(Point _a, Point _b, Point _c) { a = _a; b = _b;
c = _c;}
public String toString() { return "<" + a.toString()+
b.toString()+ c.toString() + ">";}
}

14/20
public class TriangleFile
{
public static void main(String[] args)
throws IOException, ClassNotFoundException {

Triangle t1 = new Triangle(new Point(-1,0), new
Point(1, 0), new Point(0, 1));

ObjectOutputStream out = new
ObjectOutputStream(new FileOutputStream
("myObjectFile"));
14/21
out.writeObject(t1);
out.close();

ObjectInputStream in = new ObjectInputStream (new
FileInputStream ("myObjectFile"));

Triangle t2 = (Triangle) in.readObject();

System.out.println(t2.toString());
}
}

You might also like