S
S
SaintShadow2014-08-06 14:47:06
Java
SaintShadow, 2014-08-06 14:47:06

After writing the object to the end of the file (append), reading the appended data throws a StreamCorruptedException, what are the ways to deal with it?

I wanted to write serialized objects to a file, for the test I use ordinary strings. I write them to a file, then I read them - everything is fine. I close the program and open it again, add lines to the end of the file, and then read the entire file again. Objects written in the first session are read, after which StreamCorruptedException invalid type code: AC takes off. Google suggested that this AS is written to the file before the added data. ObjectInputStream stumbles upon it and doesn't know what to do next.

public class saveMessages {

  public static void main(String[] args) throws IOException, ClassNotFoundException {
    
    BufferedReader bR = new BufferedReader(new InputStreamReader(System.in));

           FileOutputStream fos = new FileOutputStream("e:/temp.out", true);
      ObjectOutputStream oos = new ObjectOutputStream(fos);
      while(true)
      {
        String s = bR.readLine();
        if(s.equals("show"))
        {
          oos.close();
          fos.close();
          break;
        }
    oos.writeObject(s);
        }
      show();		
      
      }
  

    


public static void show() throws IOException, ClassNotFoundException
{
FileInputStream fis = new FileInputStream("e:/temp.out");
ObjectInputStream oin = new ObjectInputStream(fis);	


while (fis.available() != 0)
{
  
    
    String ts = (String) oin.readObject();
    System.out.println(ts);
  }
}
}

Saw council - to erase this AC (3 bytes). But I haven't figured out how to do it yet.
There was also advice to use someone else's class. But I don't understand how it works inside. That's why I don't really want to use it.
Are there any more reasonable methods of struggle?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Evgeny Khabarov, 2014-08-06
@ehabarov

StackOverflow offers the following method: Appending to an ObjectOutputStream
The essence of the method:
- If the final file does not exist, then we use the usual ObjectOutputStream to create it (in this case, the necessary signature is written to the beginning of the file).
- If the file already exists, we use the AppendingObjectOutputStream class proposed there, which prevents the signature from being re-formed.
UPD: Code example, moved from comment.
AppendingObjectOutputStream.java is a copy of the code from StackOverflow.
ObjectStreamTest.java is an example of usage in a program.
The writeObject() method - writes the next object to the file.
This method uses:
- ObjectOutputStream - if the file does not exist (it will be created).
- AppendingObjectOutputStream - if the file already exists (it will be added).
readObjects() method - sequentially reads a file with objects and displays the class name of each read object. Used to make sure the file is read normally after being appended.
--- AppendingObjectOutputStream.java ---

import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.OutputStream;

public class AppendingObjectOutputStream extends ObjectOutputStream 
{
  public AppendingObjectOutputStream(OutputStream out) throws IOException 
  {
    super(out);
  }
  @Override
  protected void writeStreamHeader() throws IOException 
  {
    // do not write a header, but reset:
    // this line added after another question
    // showed a problem with the original
    reset();
  }
}

---ObjectStreamTest.java ---
import java.io.EOFException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamTest 
{
  public static void main(String[] args) throws IOException, ClassNotFoundException 
  {
    String filename = "objFile.bin";
    writeObject(filename, new String("Test1"));
    writeObject(filename, new Integer(10));
    writeObject(filename, new Long(200));
    writeObject(filename, new String("Test2"));
    readObjects(filename);
  }
  public static void writeObject(String filename, Object obj) throws IOException
  {
    File file = new File(filename);
    FileOutputStream  fos = null;
    ObjectOutputStream oos = null;
    if ( file.exists() ) 
    {
      fos = new FileOutputStream(file, true);
      oos = new AppendingObjectOutputStream( fos );
    }
    else
    {
      fos = new FileOutputStream(file);
      oos = new ObjectOutputStream(fos);
    }
    oos.writeObject(obj);
    oos.close();
    fos.close();
  }
  public static void readObjects(String filename) throws IOException, ClassNotFoundException
  {
    FileInputStream  fis = new FileInputStream(filename);
    ObjectInputStream ois = new ObjectInputStream(fis);
    Object obj = null;
    try 
    {
      while ( (obj = ois.readObject() ) != null )
      {
        System.out.println("Object class is : "+obj.getClass().getName());
      }
    } 
    catch (EOFException e) 
    {
      System.out.println("End of file reached.");
    }
    ois.close();
    fis.close();
  }
}

A
a_f0x, 2014-08-07
@a_f0x

make the stream flush
public static void main(String[] args) throws IOException, ClassNotFoundException {
File file = new File("d:/temp.out");
if (!file.exists())
file.createNewFile();
BufferedReader bR = new BufferedReader(new InputStreamReader(System.in));
FileOutputStream fos = new FileOutputStream(file, true);
ObjectOutputStream oos = new ObjectOutputStream(fos);
while(true){
String s = bR.readLine();
if(s.equals("show")){
oos.flush();
fos.flush();
oos.close();
fos.close();
break;
}
oos.writeObject(s);
}
show();
}
public static void show() throws IOException, ClassNotFoundException{
File file = new File("d:/temp.out");
if (!file.exists())
file.createNewFile();
FileInputStream fis = new FileInputStream(file);
ObjectInputStream oin = new ObjectInputStream(fis);
while (fis.available()!=0){
String ts = (String) oin.readObject();
System.out.println(ts);
}
}
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question