How to Serialize an Object:

I have an Automotive class which has the following methods.

Iam able to read the i/p file and print the o/p in another file.

But when iam trying to serialize the Automotive class a .dat file is created but the o/p is not printed.

Can we do the serialize the Automotive object in the same class as i have done below?

Or should i create an instance of the class in the main() and then serialize()?

help required.

Here's the code below:

public class Automotive implements Serializable{

BufferedReader inputstream;

BufferedWriter outputstream;

StringTokenizer st;

OptionSet[] os= new OptionSet[50];//Instantiating OptionSet Class

private int coo[]=new int[50];//count of options

private String noo[]=new String[50];//array of options names

private int price[]=new int[50];//array of options price

private String type[]=new String[50];//array of option sets

private static final long serialVersionUID = 1L;

public void readFile()

{

try

{

inputstream = new BufferedReader(new FileReader("C:\\CarInputData.txt"));

outputstream = new BufferedWriter(new FileWriter("C:\\CarOutputData.txt"));

String line= inputstream.readLine();

st = new StringTokenizer(line,":");

outputstream.write("Car Model :" + st.nextToken());

outputstream.newLine();

inputstream.close();

outputstream.close();

}//end of try block

--

public void serializeAuto(Automotive a)

{

try

{

ObjectOutputStream output=new ObjectOutputStream(new FileOutputStream("C:\\CarData.dat"));

output.writeObject(a) ;

output.flush();

output.close();

}

}

--

public void deserializeAuto()

{

Automotive a;

try

{

ObjectInputStream input=new ObjectInputStream(new FileInputStream("C:\\CarData.dat"));

a = (Automotive) input.readObject();

}

}

[1991 byte] By [GitaSumana] at [2007-11-27 3:36:49]
# 1
iam getting the following error:IOException:writing aborted; java.io.NotSerializableException: java.io.BufferedReader
GitaSumana at 2007-7-12 8:40:04 > top of Java-index,Core,Core APIs...
# 2
I agree, BufferedReader is not serializable. Why do you want to serialize a BufferedReader? or a BufferedWriter? Those things should be local variables, not data members of the class, and so should the StringTokenizer among others.
ejpa at 2007-7-12 8:40:04 > top of Java-index,Core,Core APIs...