Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

ObjectOutputStream Make copy of Serialized File

 
Victor Dias
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • 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
  • 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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic