Files and Serialization

computerharpySoftware and s/w Development

Dec 2, 2013 (3 years and 9 months ago)

77 views

Files and Serialization

Files

Used to transfer data to and from secondary storage


Diskette
Memory
Input file
Output file
A Portion of Java

s File Classes

Object

InputStream

OutputStream

FileInputStream

FileOutputStream

ObjectInputStream

ObjectOutputStream

File

These classes are included in the
java.io

package,
which must be imported into any application that uses
them

Basic Algorithm for File Output

Open an output connection to a file

Write the data to the file

Close the output connection to the file


FileOutputStream

Disk

Files and Exceptions


Java requires the programmer to catch file
exceptions



For example, an
IOException

can be
thrown during at attempt to open a file

try
-
catch

Format

try{


<code that might throw an exception>

}catch(<exception type
-
1> e){


<code to handle exception of type 1>

}

.

.

catch(<exception type
-
n
> e){


<code to handle exception of type
n
>

}


The exception thrown by
try

is matched against the
parameter of each
catch

in sequence.

Writing Objects to Text Files

Student s = new Student("Ken", 10);

try{


FileOutputStream

fos

= new
FileOutputStream
("
student.txt
");



PrintWriter

writer = new
PrintWriter
(
fos
);



writer.println
(
s.getName
());


writer.println
(
s.getNumScores
());


for (
int

i

= 1;
i

<=
s.getNumScores
();
i
++)


writer.println
(
s.getScore
(
i
));


fos.flush
();


fos.close
();

}catch(Exception e){


System.out.println
("Error in output:" +
e.toString
());

}

Must agree with input process about format of text file.

Basic Algorithm for File Input

Open an input connection to a file

Read the data from the file and process it

Close the input connection to the file


FileInputStream

Disk

Reading Objects from Text Files

Student s = null;

try{


FileInputStream

fis

= new
FileInputStream
("
student.txt
");



Scanner reader = new Scanner(
fis
);




String name =
reader.nextLine
();


int

numScores

=
Integer.parseInt
(
reader.nextLine
());


s = new Student(name,
numScores
);


for (
i

= 1;
i

<=
numScores
;
i
++)


s.getScore
(
Integer.parseInt
(
reader.nextLine
));

fis.close
();

}catch(Exception e){


System.out.println
("Error in output:" +
e.toString
());

}

Must agree with output process about format of text file.

A Simpler Solution: Serialization


A data model is said to be
serialized

when it
can be permanently saved on a storage
medium



Serialization requires file management



OOP systems support object serialization

Serializing a Data Model


All classes to be serialized must implement
the
Serializable

interface



This interface is defined in the
java.io

package



Many standard java classes are already
serializable

import
java.io.Serializable
;


public class Student implements Comparable<Student>,


Serializable
{

.

.


}

Serializing the Student Class

Many of Java

s data classes are already serializable


All specialized data classes should be made to be serializable

Writing Objects to Object Files


Create a new instance of
FileOutputStream



Wrap around this object a new instance of
ObjectOutputStream



Use the method
writeObject

to send an
object out on the stream

Writing Objects to Output Files

new FileOutputStream(<a String>)

new ObjectOutputStream(<a FileOutputStream>)

<an ObjectOutputStream>.writeObject(<any object>)

A file name

Writing Objects to Output Files

Student s = new Student("Ken", 10);

try{


FileOutputStream

fos

= new
FileOutputStream
("
student.dat
");



ObjectOutputStream

oos

= new
ObjectOutputStream
(
fos
);



oos.writeObject
(s);


fos.flush
();


fos.close
();

}catch(Exception e){


System.out.println
("Error in output:" +
e.toString
());

}

Writes the whole
Student
object to the file without

any fuss.

Reading Objects from Input Files


Create a new instance of
FileInputStream



Wrap around this object a new instance of
ObjectInputStream



Use the method
readObject

to receive an
object from the stream



Cast the received object to its actual class

Reading Objects from Input Files

new FileInputStream(<a String>)

new ObjectInputStream(<a FileInputStream>)

(<class name>) <an objectInputStream>.readObject()

A file name

Reading Objects from Input Files

Student s;

try{


FileInputStream

fis

= new
FileInputStream
("
student.dat
");



ObjectInputStream

ois

= new
ObjectInputStream
(
fis
);



s = (Student)
ois.readObject
();


fis.close
();

}catch(Exception e){


System.out.println
("Error in input:" +
e.toString
());

}

Reads the whole
Student
object from the file without

any fuss.

Define a
static

Method for Input

static public
readFromFile
(
ObjectInputStream

ois
){


try{


return (Student)
ois.readObject
();


}catch(Exception e){


System.out.println
("Error in input:" +
e.toString
());


return null;


}

}

try{


// Open file input stream and object input stream


// while there are more objects to be read


studentList.add(Student.readfromFile(ois));

} catch(Exception e){ …

Alternatively, write and read the entire list!