File Input and Output

computerharpySoftware and s/w Development

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

78 views

Strategic Consulting Group

IO
-
1

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.











File Input and Output

Strategic Consulting Group

IO
-
2

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Objectives





Java File I/O


Stream classes


Readers and Writers


Random Access Files


Object Serialization

Strategic Consulting Group

IO
-
3

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Introduction


Reading and writing is accomplished using
streams

and
readers

and
writers


A

stream

can be either a source or destination of bytes and
are used to implement byte oriented I/O


readers

and
writers

implement Unicode oriented I/O


Input Output classes are defined in java.io package


We start our discussion with the examination of several
stream classes.


This is followed by examples of readers and writers, object
streams and random access files

Strategic Consulting Group

IO
-
4

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Streams


Streams can be compared to a hose that carries data


Just as different sized of pipes can be interconnected,
different types of streams can be interconnected


In general, stream classes can be thought of as node
streams and filter streams


node streams connect with devices from which a byte
stream is read or to which a byte stream is written to


filter streams connect with existing node streams and
reorganizes the data bits for use by a program


InputStream

class is responsible for reading


OutputStream

class is responsible for writing


Both these streams implement byte oriented I/O

Strategic Consulting Group

IO
-
5

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

InputStream methods


The input stream has an abstract method to read one byte,
and 2 non
-
abstract methods to read many bytes


int read()


-

read next byte


int read(byte[] b)

-

read some bytes into b


int read(byte[] b, int off, int len)
-

reads up to len bytes and store in
b starting with index = off


All of the read methods throw IOException


All of the read methods return the number of bytes read
and
-
1 at End of Stream

Strategic Consulting Group

IO
-
6

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

InputStream methods


Other available methods of input stream include


int available()
-

returns the number of bytes that can be read
without interruption by another


void close()
-

closes the stream and releases resources


void mark(int readlimit)
-

specifies a logical mark; readlimit
defines the boundary of the mark, mark is lost if readlimit is
exceeded


boolean markSupported()
-

tests if the input stream supports mark


void reset()
-

reposition the stream to the mark


long skip(long n)
-

skip n bytes

Strategic Consulting Group

IO
-
7

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

OutputStream methods


Writing to the stream is conceptually a mirror of reading


OutputStream contains an abstract write method that writes
one byte to the output stream, and two non
-
abstract
methods to write many bytes


void write(int b)
-

write the byte to the stream


void write(byte[] b)
-

write
b.length

bytes


void write(byte[] b, int off, int len)
-

write
len

bytes from starting
from
index = off


void close()
-

close the stream


void flush()
-

flushes the stream

Strategic Consulting Group

IO
-
8

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Implementing a Stream I/O


Both InputStream and OutputStream are abstract classes


Therefore, implementation of these streams are done
through subclasses


Example of subclasses of InputStream include


ByteArrayInputStream
-

Uses an internal byte array as a buffer


FileInputStream
-

Obtains input from a file


FilterInputStream
-

Obtains input from some other input stream


ObjectInputStream
-

Used to read previously serialized objects


PipedInputStream
-

Sets up a circular buffer in conjunction with a
PipedOutputStream; best used with a thread


SequenceInputStream
-

Multiple input streams can be read in
sequence

Strategic Consulting Group

IO
-
9

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Implementing a Stream I/O


Examples of subclasses of OutputStream include


ByteArrayOutputStream
-

data is written to a byte array


FileOutputStream
-

data is written to a file


FilterOutputStream
-

Interacts with other streams to transform data
from a program to a device specific stream


ObjectOutputStream
-

serializes an object’s data


PipedOutputStream
-

Couples with the PipedInputStream


Notice that most input streams have their corresponding
output stream classes


Example using FileInputStream and FileOutputStreamwith
and without buffer is shown next

Strategic Consulting Group

IO
-
10

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

CopyFileUsingByteStream.java

import java.io.*;

class CopyFileUsingByteStream {


public static void main(String [] args) {


FileInputStream fis

= null;


FileOutputStream fos

= null;


byte inByte;


try {


fis = new FileInputStream(new File(args[0]));


File

outFile = new File(args[1]);


if (outFile.exists()) {


System.out.println("File "+args[1]+" already exists");


return;


} else


fos = new FileOutputStream(args[1]);

Name of the input file

Name of the output file

Strategic Consulting Group

IO
-
11

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

CopyFileUsingByteStream.java


inByte = (byte)
fis.read();


while (inByte !=
-
1) {


fos.write(inByte);


inByte = (byte) fis.read();


}


} catch (FileNotFoundException e) {


System.out.println ("File not found "+args[0]);


} catch (IOException e) {


System.out.println(e.getMessage());


} finally {



try {


if (fis != null)


fis.close();


if (fos != null)


fos.close();


} catch (IOException e) {}



}


}

}

A source file is copied one byte
at a time

Strategic Consulting Group

IO
-
12

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

FileCopy.java


Extracts from FileCopy.java is shown


This program is an exact duplicate of the
CopyFileUsingByteStream.java, except for the buffered
read and the use of available() method





byte[] inByte;



int size;



// code to open file as in the last example




size =
fis.available();




if (size > 0) {





inByte = new byte[size];





fis.read(inByte);



fos.write(inByte);



}

Strategic Consulting Group

IO
-
13

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Data Streams


FileOutputStream and FileInputStream operate using byte
oriented output and input
-

not very useful in many cases


Programs often use ‘binary’ data other than bytes i.e int,
float


Data stream classes provide this capability


Data streams derive from Filter streams and do not interact
with devices


Data stream classes are normally chained to other streams
that do interact with a device


Data Streams provide methods to read and write all basic
java data types

Strategic Consulting Group

IO
-
14

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Data Streams


There are two data stream classes


DataOutputStream


DataInputStream


These classes are derived from FilterOutputStream and
FilterInputStream classes respectively


Examples of write/read methods include



writeChar


readChar



writeInt


readInt



writeDouble


readDouble



writeBoolean


readBoolean

Strategic Consulting Group

IO
-
15

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Data Streams


Other methods



void flush()

-

flush the output stream


The general chaining process of a data stream (filter) to a
node stream (FileInput) can be shown as follows




Node


device


Node stream

Filter
stream

Program



Node


device


Node stream

Filter
stream

Program

Strategic Consulting Group

IO
-
16

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Data Input and Output Streams


To set up a data output stream


DataOutputStream dos = new DataOutputStream(





new FileOutputStream(tempFile));



tempFile can be a File object or a String


To set up a data input stream


DataInputStream dis = new DataInputStream(





new FileInputStream(tempFile));


An example is shown next; this example generates a set of
random integers, writes them to a file and then reads them
back

Strategic Consulting Group

IO
-
17

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestDataStream.java

import java.io.*;

class TestDataStream {


public static void main(String [] args) {


DataInputStream dis

= null;


DataOutputStream dos

= null;



long writeTime=0, readTime=0;



long swTime, ewTime, srTime, erTime;


File tempFile = new File("temp.dat");


if (tempFile.exists()) {


System.out.println("File temp.dat already exists");


System.out.println("Delete the file and rerun the program");


System.exit(0);


}


try {


dos = new DataOutputStream(new FileOutputStream(tempFile));



swTime = System.currentTimeMillis();

Notice chaining of streams

Variables used to
measure read write
efficiency

Strategic Consulting Group

IO
-
18

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestDataStream.java


for (int i =0; i < 10000; i++) {


dos.writeInt((int)(Math.random()*1000));


}



ewTime = System.currentTimeMillis();



writeTime = ewTime
-

swTime;


} catch (IOException e) {


System.out.println(e.getMessage());


} finally {


try {


if (dos != null)


dos.close();


} catch (IOException e) {}


}


// read data


try {


dis = new DataInputStream(new FileInputStream(tempFile));




srTime = System.currentTimeMillis();

Chaining of streams on the input side

Strategic Consulting Group

IO
-
19

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestDataStream.java


for (int i = 0; i < 10000; i++)


System.out.print(" "+
dis.readInt()
);



erTime= System.currentTimeMillis();



readTime = erTime
-

srTime;


} catch (FileNotFoundException e) {


System.out.println("File not found");


} catch (IOException e) {


System.out.println(e.getMessage());


} finally {



System.out.println("
\
nWrite = "+writeTime+" Read = "+readTime);


try {


if (dis != null)


dis.close();


} catch (IOException e) {}


}


}

}

Timing discussed
after the next
example

Strategic Consulting Group

IO
-
20

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Efficiency of Data Streams


The I/O, as used in the TestDataStream.java, is still byte
oriented and therefore, disk access intensive


Efficiency can be improved by buffering I/O


To set up a buffered stream, we can add a buffering layer
to the data stream


DataOutputStream dos = new DataOutputStream(







new BufferedOutputStream(






new FileOutputStream(tempFile)));



Buffered Streams are also filter classes and provide
internal data buffering


Notice chaining of streams through constructors

Strategic Consulting Group

IO
-
21

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Extracts from TestBufferedDataStream.java

import java.io.*;

class TestBufferedDataStream {


public static void main(String [] args) {


DataInputStream dis = null;


DataOutputStream dos = null;


// set up code


try {


dos =
new DataOutputStream( new BufferedOutputStream(






new FileOutputStream(tempFile)))
;



for (int i =0; i < 10000; i++)


dos.writeInt((int)(Math.random()*1000));



}



// other code


try {


dis =
new DataInputStream(new BufferedInputStream(






new FileInputStream(tempFile)));


for (int i = 0; i < 10000; i++)


System.out.print(" "+dis.readInt());



// rest of the code


}

See source file for the full code

Strategic Consulting Group

IO
-
22

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Efficiency of Data Streams


What kind of efficiency is gained?


You will recall that we had embedded time collection
statements in the TestDataStream program


We did the same for the next program
TestBufferedDataStream.java which uses a buffered
stream; both programs are otherwise comparable





Write Time(ms)

Read Time (ms)



Unbuffered

990


9280



Buffered


50


7850


Better timing for read can be computed by removing the
println that embeds the readInt call

Strategic Consulting Group

IO
-
23

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Text Streams


DataStreams are fine if a data file in binary encoded form
is already there


It is quite common to read and write human
-
readable data
file


Text file I/O can be done using descendants of
Reader

and
Writer

classes


InputStreamReader

can be used to read a text stream


OutputStreamWriter

can be used to write a text stream


As in the case of data streams, these streams need to be
chained to a node stream class

Strategic Consulting Group

IO
-
24

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Text Streams


An InputStreamReader can be setup with the following:

InputStreamReader in = new InputStreamReader(
System.in
);


System.in enables a program to read data from the console

InputStreamReader in = new InputStreamReader(




new FileInputStream(filename));


Text data can be read from a file, notice chaining


Text reading can also be buffered

BufferedReader br = new BufferedReader(




new InputStreamReader(new FileInputStream(






new File(filename))));

Strategic Consulting Group

IO
-
25

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Encoding Scheme


InputStreamReader and OutputStreamWriter classes can be
instantiated with an optional encoding scheme

InputStreamReader in = new InputStreamReader(




new FileInputStream(filename), encString);



filename can be a File object or a filename string



encString specifies an encoding String



8859_1”


ISO Latin
-
1

default for Windows


“Big5”


Traditional Chinese


“Cp1253”


Windows Greek


“Cp863”


PC Canadian French

Strategic Consulting Group

IO
-
26

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Reading and writing Text files


It is common to associate text oriented I/O with some file


Special Writer classes exist to write to a file in text format


PrintWriter out = new PrintWriter(




new FileWriter("testdata.dat"));


PrintWriter class provides a number of overloaded
methods for print, println, write for text output


There is no PrintReader class, but only a FileReader class


BufferedReader class has a readLine() method to read an
entire line terminated by a new line character




Strategic Consulting Group

IO
-
27

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Reading Text files


Text reading from a file can be set up using



File input = new File("testdata.dat");



FileReader in = new FileReader(input);




BufferedReader br = new BufferedReader(in);


br.readLine();
-

returns a String equal to the line terminated
by
\
r or
\
n, or null at the end of stream


StringTokenizer class can be used to break the line read
into tokens provided the string has some delimiter

StringTokenizer stk = new StringTokenizer(s,delim);

where the delim is the delimiter string

Strategic Consulting Group

IO
-
28

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestBufferedReads.java

public class TestBufferedReads {


public static String getConsoleLine(BufferedReader br) throws IOException {



return br.readLine();


}


public static void writeDiskFile(PrintWriter out, String s) throws IOException {



out.println(s);


}


public static String[] getColumns(BufferedReader br,String delim)





throws NoSuchElementException, IOException {



String s = br.readLine();



if ( s == null) return null;



StringTokenizer stk = new StringTokenizer(s,delim);



int tokenCount = stk.countTokens();



String [] words = new String[tokenCount];



for (int i =0; i < tokenCount; i++) {




words[i] = stk.nextToken();



}



return words;


}

Strategic Consulting Group

IO
-
29

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestBufferedReads.java


public static void main(String [] args) {



InputStreamReader in = null;



BufferedReader br = null;



FileReader fr = null;



PrintWriter out = null;



FileWriter fw = null;



String inText;



String [] fields;



String fname = null, lname=null;



double salary=0;



File input = null;



try {




in = new InputStreamReader(System.in);




br = new BufferedReader(in);




fw = new FileWriter("testdata.dat");




out = new PrintWriter(fw);




System.out.println("Input for setting up a data file");




System.out.println("Enter three strings: first name, last name, salary");




System.out.println("Separate fields with | or a space");

Strategic Consulting Group

IO
-
30

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestBufferedReads.java




System.out.println("Terminate input with 'end'");




inText = getConsoleLine(br);




while (!(inText.equals("end"))) {




writeDiskFile(out,inText);




inText = getConsoleLine(br);




}



} catch (IOException e) {




System.out.println(e.getClass().getName()+" "+e.getMessage());



} finally {




try{




if (br != null)




br.close();




if (out != null) {




out.flush();




out.close();




}

Strategic Consulting Group

IO
-
31

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestBufferedReads.java




}catch (IOException f) {




f.printStackTrace();




}



}


try {



input = new File("testdata.dat");



in = new FileReader(input);




br = new BufferedReader(in);



fields = getColumns(br," |");



while ( fields != null) {



fname = fields[0];



lname = fields[1];



salary = Double.parseDouble(fields[2]);



System.out.println("name = "+lname+", "+fname+" salary: "+salary);



fields = getColumns(br," |");



}

Strategic Consulting Group

IO
-
32

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestBufferedReads.java


} catch (IOException e) {



System.out.println(e.getClass().getName()+" "+e.getMessage());


} finally {



try {





if (br != null){




br.close();




}



} catch (IOException f) {




f.printStackTrace();



}


}



}

}

Strategic Consulting Group

IO
-
33

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

File


Examples used in this section have liberally used files; a
brief description of file class is, therefore, in order


The stream and reader/writer classes use files to read from
and write to


But a program often needs to know, for example, location
of the file, readability, file size, length of contents etc


A File object is the answer to many of these questions


A File object is created using one of 3 constructors


File(String pathname);


File(File parent, String child);


File(String parent, String child);

Strategic Consulting Group

IO
-
34

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

File


A number of methods exist to manipulate a File object


boolean canRead()
-

Can the file be read


boolean canWrite()
-

Can the file be written to


int compareTo(File pathname)
-

compare two filenames


boolean createNewFile()
-

Create a new file only if not there


boolean delete()
-

Delete the file or directory


boolean exists()
-

Tests if file is there


String getName()
-

Return the pathname of the file or directory


boolean isDirectory()
-

Is the object a directory


boolean isAbsolute()
-

Tests for absolute pathname


long length()
-

Length of the file in bytes


String[] list()
-

list of files and directories for the object; object
should be a directory

Strategic Consulting Group

IO
-
35

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Random Access Files


Java provides a Random Access file class that can be used
to access a record directly


RandomAccessFile raf = new RandomAccessFile(ra,mode);


ra
-

A file object or a file name


mode
-

usage mode: “r” or “rw”


The file pointer can be set to any location with a seek(long)


Program must provide a means of computing the offset
where a record ends and the next record begins


Those interested are referred to the TestRandomFile.java
available in the examples directory

Strategic Consulting Group

IO
-
36

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Object Persistence


Programs sometimes need to save an Object to a
permanent storage to be used by another program


Object can be stored to a file by ‘serializing’ the Object


An ObjectOutputStream is used to write Objects


ObjectOutputStream os = new ObjectOutputStream(



new FileOutputStream(new File(filename)));


An ObjectInputStream is used to read Objects


ObjectInputStream is = new ObjectInputStream(



new FileInputStream(new File(filename)));

Strategic Consulting Group

IO
-
37

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Object Persistence


Basic method to write to an Object stream

writeObject(Object o)

writeInt(int i)
-

Other methods exist to write other
primitives


Basic method to read from an Object stream

readObject()
-

returns an Object

readInt()
-

returns a primitive int; other such methods are
available


Strategic Consulting Group

IO
-
38

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Serializing an Object


All Objects cannot be serialized


An Object can be serialized by implementing Serializable
interface


public class ObjectStack implements Serializable {




//


}


Serializable interface does not require implementation of
any special method and is considered a marker interface

Strategic Consulting Group

IO
-
39

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

What is Serialized


Only data is serialized, not any method


Serialization of specific data members of an Object can be
prevented by marking the data as
transient


public class Account implements Serializable {



private
transient

long accountNumber;



private
transient

long accountBalance;



private String accountName;


}


Seralized objects of type Account would not include
accountNumber or accountBalance

Strategic Consulting Group

IO
-
40

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestObjectSerialization.java

import java.io.*;

import java.util.*;

public class TestObjectSerialization {


public static void serializeObject(Object o, ObjectOutputStream os)






throws IOException {



os.writeObject(o);


}


public static Object unSerializeObject(ObjectInputStream is)




throws ClassNotFoundException, IOException {



return(is.readObject());


}


public static void main(String[] args) {



if (args.length < 1) {



System.out.println("Usage java TestObjectSerialization "+ ” ” +









"<output>");



return;



}

Strategic Consulting Group

IO
-
41

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestObjectSerialization.java


ObjectOutputStream os = null;


ObjectInputStream is = null;


// A number of hard coded object to be serialized


int i = 3;


float f = 3.4f;


String s = "Java String";


Date d = new Date();


char [] c = {'A','B','C','D','E'};


ObjectStack st = new ObjectStack();


st.push(d);


st.push(c);


st.push(s);


try {



os = new ObjectOutputStream( new FileOutputStream(new File(args[0])));



serializeObject(new Integer(i),os);



serializeObject(new Float(f),os);

Strategic Consulting Group

IO
-
42

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestObjectSerialization.java


serializeObject(s,os);


serializeObject(d,os);


serializeObject(c,os);


serializeObject(st,os);


os.writeInt(i);


os.writeFloat(f);

} catch (IOException e) {



System.out.println("Error: "+e.getClass().getName()+" "+e.getMessage());

} finally {



try {




if (os != null)





os.close();



} catch (IOException e1) {}

}

Strategic Consulting Group

IO
-
43

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestObjectSerialization.java


// Unserialize, we know the order of serialization


try {



is = new ObjectInputStream(new FileInputStream(new File(args[0])));



System.out.println("Integer: "+unSerializeObject(is));



System.out.println("Float: "+unSerializeObject(is));



System.out.println("String: "+unSerializeObject(is));



System.out.println("Date: "+unSerializeObject(is));



System.out.println("Char array: "+new


String((char[])unSerializeObject(is)));



System.out.println("Object stack: "+unSerializeObject(is));



System.out.println("Primitive int: "+is.readInt());



System.out.println("Primitive float: "+is.readFloat());


} catch (ClassNotFoundException cne) {



System.out.println("Error: "+cne.getClass().getName()+” ”








+cne.getMessage());


} catch (IOException e) {

Strategic Consulting Group

IO
-
44

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestObjectSerialization.java


System.out.println("Error: "+e.getClass().getName()+" "+e.getMessage());


} finally {


try {


if (is != null)




is.close();



} catch (IOException e1) {}


}


}

} // end TestObjectSerialization class


class ObjectStack implements Serializable {


private Vector v = new Vector(100,20);


public void push(Object n) {



v.add(n);


}


public String toString() {



return ("ObjectStack with size = "+getHeight());


}

Strategic Consulting Group

IO
-
45

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

TestObjectSerialization.java


public Object pop() {



Object i;



int ht;



while(v.isEmpty()) {




try {




this.wait();




} catch (InterruptedException ie) {




System.out.println(ie.getClass().getName()+" "+ie.getMessage());




}



}



ht = getHeight();



i = v.remove(ht
-
1);



return(i);


}


public int getHeight() {



return v.size();


}

}

Strategic Consulting Group

IO
-
46

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Versioning Problem


Consider the following scenario


Serialization design is done at Time A and code distributed


A program runs and serializes the object at Time B which is after
Time A


Between Time A and B, the object design is changed; the code to
unserialize is not changed


THUS WHEN AN OBJECT IS READ BACK, THE DESIGN IS
A VERSION OLD


If version mismatch occurs, the program throws an
exception


How do we handle such version problem?

Strategic Consulting Group

IO
-
47

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Versioning Problem


The obvious solution to versioning problem is to
redistribute the byte code when versions are updated.


Redistribution does not solve the problem of reading back
old serialized data


We can use the concept of version Identification


Every class is associated with a version Id


The version id for a class can be found by executing the
serialver

command




>serialver ByteCodeFile


The command yields a very long integer i.e the serial id

Strategic Consulting Group

IO
-
48

© Copyright 1999
-
2000 by Santosh Misra. All rights
reserved. May not be reproduced in whole or in part by any
means without prior written permission.

Versioning Problem


The serial version id can be embedded in the class
definition


public class Account implements Serializable {



private final static long serialVersionUID =




3745358160550433574L;




// old data members




// new data members



}


The unserialization process uses the serialVersionUID
value instead of generating a new serialized id


New data members are ignored