Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8783423
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T20:49:41+00:00 2026-06-13T20:49:41+00:00

I want to serialize a same object on file, each time when i run

  • 0

I want to serialize a same object on file, each time when i run a program. This is a simple algorithm to explain my problem.

In the begening i store a String on writer. in the last i read a file.
The goal of this program is if i run my program X time, i store and i print on screen X time my object.

class ReadFile {

    static ObjectOutputStream writer = null;

    public static void main(String[] args) throws IOException, ClassNotFoundException {

        writer = new ObjectOutputStream(new FileOutputStream("trace", true));
        store("String");

        if (writer != null) {
            writer.close();
        }

        open("file.tmp");

    }

    static public void store(String chaine) {
        if (writer == null) {
            return;
        }
        try {
            writer.writeObject(chaine);
        } catch (IOException ex) {
        }
    }

    static public void open(String file) throws FileNotFoundException, IOException, ClassNotFoundException {
        StringBuilder str = new StringBuilder();
        ObjectInputStream objs;
        try {

            objs = new ObjectInputStream(new FileInputStream(file));
            try {
                while (true) {
                    Object obj = objs.readObject();
                    str.append(obj.toString());
                }
            } catch (EOFException ex) {
            }
            objs.close();

        } catch (Exception ex) {
            ex.printStackTrace(System.err);
        }
    }
}

When i run this program i have this error :

java.io.StreamCorruptedException: invalid type code: AC
        at java.io.ObjectInputStream.readObject0(ObjectInputStream.java:1355)
        at java.io.ObjectInputStream.readObject(ObjectInputStream.java:350)
        at ReadFile.open(ReadFile.java:47)
        at ReadFile.main(ReadFile.java:35)

What can i do please ?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T20:49:42+00:00Added an answer on June 13, 2026 at 8:49 pm

    According to this post you cannot append to a ObjectOutputStream, which you are trying to do by opening the underlying FileOutputStream in append mode. There is a solution mentioned on that post such that you create an AppendableObjectOutputStream , or you could just open the FileOutputStream without appending.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to serialize an object and store it inside sdcard under my project
I want to Serialize a 60mb file into XML but it gives me System
I want to serialize this class: public class CarDisplay { public string Name {
I want to serialize an object into an xml and I want the filename
I want to serialize an object of type Person . I want to use
i want to serialize this class, to store it on the sdcard of the
I want to serialize an object as xml that contains other custom classes. From
I want to serialize a hash map to a file and de-serialize it later
I need to serialize an XML java object to a XML file using the
I want to serialize a collection of objects for storage/retrieval from my app.config file.

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.