wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Problem with serializing a class Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Spring in Action this week in the Spring forum!
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Problem with serializing a class" Watch "Problem with serializing a class" New topic
Author

Problem with serializing a class

Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi,
Could anybody look at the codes below and tell me where I have gone wrong.I am experimenting with the ObjectOutputStream.I have a class student whose state I want to serialize(in class testfile) and deserialize (in class custom ).Output .....only first record is written moreover when I adding true in construction of ObjectOutputStream then nothing is written. why???
Thanks in adv
Sonu

(edited by Cindy to format code)
[ March 04, 2002: Message edited by: Cindy Glass ]
Roy Ben Ami
Ranch Hand

Joined: Jan 13, 2002
Posts: 732
you need to close the streams after writing and after reading.
add this line after writing to the file:
oo.close();
fo.close();

and this after reading:
oi.close();
fi.close();
the most important thing to remember about streams is to close them after you are done writing/reading.
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Sonu,
There seems to be a problem regarding ObjectOutputStream and writing to files. When the writing happens it writes out a header. When your second write happens it also writes a header. The problem comes about when you try and read in the objects. The ObjectInputStream only expects a single header and not multiple headers. IMO the only work around is to read and rewrite the entire file and append the new object at the end. The following code should work if you place it into your submit section.

Regards,
Manfred.
Sandeep Ghosh
Ranch Hand

Joined: Jan 23, 2002
Posts: 145
Hi Manfred,
Thanks a lot Manfred.I have incoperated the changes mention by you but then also it is not giving any result.Now when second record is added application gets struck.Could you tell me the reason of this problem
import java.awt.*;
import java.awt.event.*;
import java.io.*;
import java.util.*;
class student implements Serializable
{
String profile;
public void getdata(String s)
{
profile=s;
}
public String showdata()
{
return profile;
}
}
//writing data to file myfile
public class testfile extends Frame implements ActionListener
{
TextArea ta;
Button submit,show;
custom c;
FileOutputStream fo;
ObjectOutputStream oo;
student stud;
File fileIn,fileOut;
FileInputStream fi;
ObjectInputStream in ;

public testfile()
{
stud=new student();
ta=new TextArea(20,20);
submit=new Button("SUBMIT");
show=new Button("SHOW");
setLayout(new FlowLayout());
add(ta);
add(submit);
add(show);
submit.addActionListener(this);
show.addActionListener(this);
addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
}

public void actionPerformed(ActionEvent e)
{
if (e.getSource()==submit)
{
try
{
stud.getdata(ta.getText());
fileIn = new File( "myfile" );
fileOut = new File( "temp.dat" ); //first append the old values to temp file
fo = new FileOutputStream( "temp.dat" );
oo = new ObjectOutputStream( fo );
// Read in old values and rewrite them out.
if( fileIn.exists() )
{
fi = new FileInputStream( fileIn );
in = new ObjectInputStream( fi );
student s = (student) in.readObject();
while( s!=null )
{

try{
s = (student) in.readObject();
oo.writeObject( s );
System.out.println("Just checking \t"+ s.showdata());
}catch(IOException eo){}
}
}

// Write new object to end of file.
oo.writeObject( stud );
System.out.println("Just checking \t"+stud.showdata());
ta.setText( "" );
oo.flush();
fo.close();
fileOut.renameTo( fileIn );
}
catch(Exception ioe ){}
finally{
try{
in.close();
fi.close();
fileIn.delete();
}catch(Exception eo){}
}
}

if (e.getSource()==show)
{
new custom();
}
}

public static void main(String args[])
{
testfile t=new testfile();
t.setSize(400,400);
t.setVisible(true);
}
}
//reading data from myfile
class custom extends Frame implements ActionListener
{
TextArea ta;
Button next,hide;
FileInputStream fi;
ObjectInputStream oi;
Vector v;
student s;
Enumeration en;
Panel p;
public custom()
{
ta=new TextArea(20,20);
p=new Panel();
next=new Button("NEXT");
hide=new Button("HIDE");
v=new Vector();
s=new student();
try
{
fi=new FileInputStream("myfile");
oi=new ObjectInputStream(fi);
s=(student)oi.readObject();
while (s!=null)
{
System.out.println(s.showdata());
v.addElement(s);
s=(student)oi.readObject();
}
}
catch(Exception ef){}
finally{
try{
fi.close();
oi.close();
}catch(Exception ef ){}
}
next.addActionListener(this);
hide.addActionListener(this);
en=v.elements();
if (en.hasMoreElements())
{
student su=(student)en.nextElement();
ta.setText(su.showdata());
}
p.add(next);
p.add(hide);
add(p,BorderLayout.SOUTH);
add(ta);
setSize(400,400);
setVisible(true);
}
public void actionPerformed(ActionEvent e)
{

if (e.getSource()==next)
{
if(en.hasMoreElements())
{
student su=(student)en.nextElement();
ta.setText(su.showdata());
}
}
if (e.getSource()==hide)
{
setVisible(false);
}
}
}
Manfred Leonhardt
Ranch Hand

Joined: Jan 09, 2001
Posts: 1492
Hi Sonu,
Your incorporation failed. You didn't copy the code exactly as I gave it to you in the writing portion of your code. You left out the most important part:

Why does this work. Well we want the while loop to continue until an I/O error occurs. This will happen when we try to read a non-existing object from the file. That seems to be the only way to figure out that we are done!
Your way: checking for s != null won't always work because the failure to read object stops the assignment so s just has the last good value in it until the end of time.
Use my code and all will work well.
Regards,
Manfred.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Problem with serializing a class