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
posted
0
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
posted
0
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
posted
0
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 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()==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
posted
0
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://ej-technologies/jprofiler - if it wasn't for jprofiler, we would need to
run our stuff on 16 servers instead of 3.