Win a copy of Head First Android this week in the Android forum!
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Tim Cooke
  • Campbell Ritchie
  • Paul Clapham
  • Ron McLeod
  • Liutauras Vilda
Sheriffs:
  • Jeanne Boyarsky
  • Rob Spoor
  • Bear Bibeault
Saloon Keepers:
  • Jesse Silverman
  • Tim Moores
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
Bartenders:
  • Piet Souris
  • Al Hobbs
  • salvin francis

ObjectOutputStream Make copy of Serialized File

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I am serializing two objects to two different files.I then copy the first object into a third file and append the second object thus saving both objects into one single file.While copying I do not use readObject as I do not want to change the instance of the object just make a copy. When I read it again I use read object but it throws a exception.Why is an exception(StreamCorruptedException) being thrown?
The code used is shown below
import java.io.*;
import java.util.Date;

public class WriteCheck
{

public static void main(String args[])
{
WriteCheck wc = new WriteCheck();
wc.write1();
wc.write2();
wc.copy();
wc.read();
}//main

public void write1()
{
try
{
FileOutputStream fos = new FileOutputStream( "Untitled1" );
ObjectOutputStream dos = new ObjectOutputStream(fos);
dos.writeObject(new Date());
dos.flush();
dos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}//write
public void write2()
{
try
{
FileOutputStream fos = new FileOutputStream( "Untitled2" );
ObjectOutputStream dos = new ObjectOutputStream(fos);
dos.writeObject(new Date());
dos.flush();
dos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}//write
public void copy()
{
try
{
FileOutputStream fos = new FileOutputStream( "Untitled3");
// ObjectOutputStream dos = new ObjectOutputStream(fos);
FileInputStream fis1 = new FileInputStream("Untitled1" );
byte[] buffer1 = new byte[4096];
int bytes1_read;
while((bytes1_read = fis1.read(buffer1))!=-1)
{
fos.write(buffer1,0,bytes1_read);
fos.flush();
}
fis1.close();
FileInputStream fis2 = new FileInputStream("Untitled2" );
byte[] buffer2 = new byte[4096];
int bytes2_read;
while((bytes2_read = fis2.read(buffer2))!=-1)
{
fos.write(buffer2,0,bytes2_read);
fos.flush();
}
fis2.close();
fos.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}//copy
public void read()
{
try
{
FileInputStream fis = new FileInputStream("Untitled3" );
ObjectInputStream ios = new ObjectInputStream(fis);
ios.readObject();
ios.readObject();
ios.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}//read
}//writeCheck
 
Victor Dias
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I managed to work this out.Hoping somebody else will find the answer useful below is what I have done
As the ObjectOutputStream adds a header everytime it starts writing and the ObjectInputStream expects to read a header everytime you initialize the stream.I just create a new ObjectInputStream for every Object written with a different ObjectOutputStream.
Thus my read code becomes
public void read()
{
try
{
FileInputStream fis = new FileInputStream("Untitled3");
ObjectInputStream ios=null;
for(int i=0;i<2;i++)
{
ios = new ObjectInputStream(fis);
ios.readObject();
}
//ios.readObject();
ios.close();
}
catch(Exception ex)
{
ex.printStackTrace();
}
}//read
 
You showed up just in time for the waffles! And this tiny ad:
Building a Better World in your Backyard by Paul Wheaton and Shawn Klassen-Koop
https://coderanch.com/wiki/718759/books/Building-World-Backyard-Paul-Wheaton
reply
    Bookmark Topic Watch Topic
  • New Topic