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 5992903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T23:35:56+00:00 2026-05-22T23:35:56+00:00

I want to reset ObjectOutPutStream to update the new object state. But why it

  • 0

I want to reset ObjectOutPutStream to update the new object state. But why it doesn’t effect. The below code outputs “BEFORE” instead of “AFTER”? What’s wrong with my code?


package test;

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class Serialization {
    public static void main(String[] strs) {
        String filename = "E:\\myObject.data";

        FileOutputStream fos = null;
        ObjectOutputStream out = null;
        MyObject myObject = new MyObject();
        try {

            myObject.setValue("BEFORE");
            fos = new FileOutputStream(filename);
            out = new ObjectOutputStream(fos);
            out.writeObject(myObject);
            out.reset();
            myObject.setValue("AFTER");
            out.writeObject(myObject);
            out.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        }
        try {
            FileInputStream fis = null;
            ObjectInputStream in = null;
            fis = new FileInputStream(filename);
            in = new ObjectInputStream(fis);
            myObject = (MyObject) in.readObject();
            System.out.println(myObject.getValue());
            in.close();
        } catch (IOException ex) {
            ex.printStackTrace();
        } catch (ClassNotFoundException ex) {
            ex.printStackTrace();
        }
    }

    public static class MyObject implements Serializable {

        private static final long serialVersionUID = 5222199410120362372L;
        private String value;

        public void setValue(String value) {
            this.value = value;
        }

        public String getValue() {
            return value;
        }
    }
}


enter image description here

  • 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-05-22T23:35:57+00:00Added an answer on May 22, 2026 at 11:35 pm

    You are writing two copies of an object but only reading one. If you read two objects you will see BEFORE and AFTER.

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

Sidebar

Related Questions

I want to reset an object. Can I do it in the following way?
I want to reset two NSTimers with a new TimeInterval. It would be no
Hey I want to reset the form but i don't know how. i mean
Suppose I want to reset a port to its initial state, including its configuration
I want to reset stop word list in mysql for FULLTEXT search. I have
I want to reset a file upload field when the user selects another option.
I have a three-dimensional array that I want to reset to zero. It seems
I have multiple input textboxes in my page. I want to reset particular text
How do I reset my numbers after they count? I want something like an
I want to reset the form after calling an ajax function. This is the

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.