Serialize an Object

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

}

}

[1985 byte] By [GitaSumana] at [2007-11-27 3:37:32]
# 1
Iam getting the following error:IOException:writing aborted; java.io.NotSerializableException: java.io.BufferedReader
GitaSumana at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 2
You have try blocks without catch blocks or a finally block?
DrLaszloJamfa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 3

public class Automotive implements Serializable{

BufferedReader inputstream;

BufferedWriter outputstream;

StringTokenizer st;

...

}

This is an error in class design. You took what were meant to be local variables in one method

and promoted them to be instance variables. Turn them back into local variables.

DrLaszloJamfa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 4

The error gone ater i made the variables as local.

But After serialization and deserialization i want to see the data in my output

file(CarOutputData.txt).

But in my CarData.dat file i am able to see some ASCII text which is not readable.

Where am i going wrong in my serialization?

Given below is my Output File CarOutputData.txt:

Car Model :Focus Wagon ZTW model

Base Price:18,445

No of option Sets: 5

OptionSet Name : Color

Number of Options for Optionset Color is 10

Nameof Option : Fort Knox Gold Clearcoat Metallic

Price of Option : 0

GitaSumana at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 5

I am confused by what you write. Serialized data is binary, not text:

import java.io.*;

public class SerExample implements Serializable {

private String message;

public void setMessage(String message) {

this.message = message;

}

public String getMessage() {

return message;

}

public String toString() {

return getMessage();

}

public static void main(String[] args) throws Exception {

SerExample obj1 = new SerExample();

obj1.setMessage("Hello, World!");

File file = new File("temp.ser");

ObjectOutputStream out = new ObjectOutputStream(new FileOutputStream(file));

out.writeObject(obj1);

out.flush();

out.close();

ObjectInputStream in = new ObjectInputStream(new FileInputStream(file));

SerExample obj2 = (SerExample) in.readObject();

in.close();

System.out.println(obj2);

}

}

Take a look at temp.ser after running this code.

DrLaszloJamfa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 6
Iam able to print String message Hello World in my temp.dat file.But my doubt is how to serialize a file?Is there any method to pass the file name to the setmethod and get the filename?I don't know how to pass file name to a method?
GitaSumana at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 7
> I don't know how to pass file name to a method?You don't know how to pass a String to a method?
DrLaszloJamfa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 8

I know how to pass a string but how to give the path for the file.

public void setFile(String path)

{

this.path=path;

}

public String getFile()

{

return path;

}

Iam doing like this in my main.

Automotive a= new Automotive();

a.readFile();

a.setFile("C:\\CarOutputData.txt");

a.serializeAuto(a);

a.deserializeAuto();

Is it right?

GitaSumana at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 9

I would Keep the Automobile class simple, and remove field "path"

and methods serialize and deserialize, placing them in another class:

Automobile a = new Automobile();

Serializer ser = new Serializer();

ser .setFile(path);

ser .serialize(a);

..

Automobile b = (Automobile) ser.deserialize();

DrLaszloJamfa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 10

Automotive a= new Automotive();

FileInput f1= new FileInput();

f1.readFile();

f1.serializeAuto(a);

Automotive b = (Automotive) f1.deserializeAuto();

Iam getting an error at the last line:

Cannot cast from void to Automotive.

I have declared the Deserialize method as void.

FileInputClass has following methods:

readFile()

serializeAuto(Automotive a);

void deserializeAuto()

GitaSumana at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 11

> Automotive b = (Automotive) f1.deserializeAuto();

>

> Iam getting an error at the last line:

>

> Cannot cast from void to Automotive.

>

> I have declared the Deserialize method as void.

So don't declare it as void. Declare it as returning an Object, or an Automotive even.

ejpa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 12
public static void main(String[] args) throws IOException {Automotive a= new Automotive();FileInput f1= new FileInput();f1.readFile();f1.serializeAuto(a);f1.deserializeAuto();Still iam not able to see the deserialized data?What is wrong.?
GitaSumana at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 13
What did you expect? Now you're throwing the deserialized object away.Your code further up is correct once you do what I said to the deserialize method.
ejpa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 14
I'm confused as to why you have separate methods readFile and deserializeAuto. Explain why you need two.
DrLaszloJamfa at 2007-7-12 8:40:48 > top of Java-index,Java Essentials,Java Programming...
# 15

I have a requirement to serialize and deserialize an object.

I have a FileInput class which has 3 methods called:

{

readFile(Automotive a)

{

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

a1.setName(st.nextToken());

outputstream.write("Car Model :" + a1.getName());

outputstream.newLine();

}

-

public void serializeAuto(Automotive a1)

{

try {

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

output.writeObject(a1) ;

output.flush();

output.close();

}

catch(IOException er )

{

}

}

-

public void deserializeAuto()

{

try

{

BufferedWriter outputstream1 = new BufferedWriter(new FileWriter("C:\\CarOutputData1.txt"));

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

Automotive a2 = (Automotive) input.readObject();

outputstream1.write(" DESERIALISED OUTPUT");

outputstream1.newLine();

outputstream1.newLine();

outputstream1.write(a2.getName());

outputstream1.newLine();

outputstream1.newLine();

int tot=a2.getTotopt();

for(int i=1;i<=tot;i++)

{

outputstream1.write("OptionSet Name : " + a2.type);

outputstream1.newLine();

outputstream1.write("Number of Options for Optionset " + a2.type +" " + "is" +" " + a2.coo);

outputstream1.newLine();

outputstream1.newLine();

a2.os.getName();

outputstream1.write(a2.os.getName());

outputstream1.newLine();

}//end of i loop

outputstream1.close();

}

catch (IOException e)

{

System.out.println("IOException:" + e.getMessage());

}

catch (ClassNotFoundException fnf)

{

System.out.println("Classnotfoundexp:" + fnf.getMessage());}

}// end of deserialize method

}

My readFile method should take input from a text file write it into an Automotive object and the deserilizeAuto() method should deserialize that Automotive object.

But when iam trying to deserialize the object iam getting the following error:

IOException:writing aborted; java.io.NotSerializableException: Automotive.OptionSet

Previously i had this error because of instance variables.But it was gone when i made them local.

Where am i going wrong?

GitaSumana at 2007-7-21 20:51:16 > top of Java-index,Java Essentials,Java Programming...
# 16
Why do we use private static final long serialVersionUID = 1L;in java.
GitaSumana at 2007-7-21 20:51:16 > top of Java-index,Java Essentials,Java Programming...
# 17
http://java.sun.com/j2se/1.5.0/docs/guide/serialization/spec/class.html#4100
ejpa at 2007-7-21 20:51:16 > top of Java-index,Java Essentials,Java Programming...
# 18

1. Method readFile makes no sense. It opens a text file and reads a "name"

from it, assigning it to the name property of a given automobile. Then it

turns around and write that name to another text file. Perhaps that method

makes sense to you, but I don't see the point.

2. serializeAuto make sense, although you should *never* do this:

catch (AnyException e) {

//no code

}

because, when errors occur, you will be ignorant of them.

3.IOException:writing aborted; java.io.NotSerializableException: Automotive.OptionSet

OuptionSet is not serializable. Do you understand the error message?

What does this tell you? How will you solve this?

DrLaszloJamfa at 2007-7-21 20:51:16 > top of Java-index,Java Essentials,Java Programming...