strategic consulting group io-1 © copyright 1999-2000 by santosh misra. all rights reserved. may...

48
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. Strategic Consulting Group File Input and Output

Post on 19-Dec-2015

212 views

Category:

Documents


0 download

TRANSCRIPT

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.

Strategic Consulting Group

File Input and Output

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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); }

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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 includewriteChar readCharwriteInt readIntwriteDouble readDoublewriteBoolean readBoolean

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.

Strategic Consulting Group

Data Streams

Other methodsvoid 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

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.

Strategic Consulting Group

Data Input and Output Streams

To set up a data output streamDataOutputStream dos = new DataOutputStream(

new FileOutputStream(tempFile)); tempFile can be a File object or a String

To set up a data input streamDataInputStream 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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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 streamDataOutputStream 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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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 9280Buffered 50 7850

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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 consoleInputStreamReader in = new InputStreamReader(

new FileInputStream(filename)); Text data can be read from a file, notice chaining Text reading can also be bufferedBufferedReader br = new BufferedReader(

new InputStreamReader(new FileInputStream( new File(filename))));

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

Reading Text files

Text reading from a file can be set up usingFile 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 delimiterStringTokenizer stk = new StringTokenizer(s,delim);where the delim is the delimiter string

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.

Strategic Consulting Group

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;

}

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.

Strategic Consulting Group

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");

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.

Strategic Consulting Group

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(); }

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.

Strategic Consulting Group

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," |");

}

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.

Strategic Consulting Group

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();

}}

}}

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.

Strategic Consulting Group

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);

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

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 ObjectsObjectInputStream is = new ObjectInputStream(

new FileInputStream(new File(filename)));

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.

Strategic Consulting Group

Object Persistence

Basic method to write to an Object streamwriteObject(Object o)writeInt(int i) - Other methods exist to write other

primitives Basic method to read from an Object stream

readObject() - returns an ObjectreadInt() - returns a primitive int; other such methods are

available

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.

Strategic Consulting Group

Serializing an Object

All Objects cannot be serialized An Object can be serialized by implementing Serializable

interfacepublic class ObjectStack implements Serializable {

//}

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

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.

Strategic Consulting Group

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 transientpublic 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

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.

Strategic Consulting Group

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; }

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.

Strategic Consulting Group

TestObjectSerialization.java

ObjectOutputStream os = null;ObjectInputStream is = null;// A number of hard coded object to be serializedint 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);

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.

Strategic Consulting Group

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) {}

}

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.

Strategic Consulting Group

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) {

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.

Strategic Consulting Group

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());}

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.

Strategic Consulting Group

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();}

}

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.

Strategic Consulting Group

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?

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.

Strategic Consulting Group

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

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.

Strategic Consulting Group

Versioning Problem

The serial version id can be embedded in the class definitionpublic 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