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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T01:09:22+00:00 2026-05-23T01:09:22+00:00

I just tried to read back an object I wrote to a file. I

  • 0

I just tried to read back an object I wrote to a file. I have two functions for it.

public static Serializable_test deSerialize_object(String filename)

{

    File a = new File(filename);
    ObjectInputStream in = null;
    try{
     in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));
    }catch(IOException e){
        e.printStackTrace();
    }
    Serializable_test obj1 = null;
    try{

    obj1 = (Serializable_test) in.readObject();
    in.close();

    }catch(IOException e){
        e.printStackTrace();
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }
    return obj1;

}
public static void deSerialize_object(Serializable_test obj,String filename){
    File a = new File(filename);
    //int objcount = 0;
    ObjectInputStream in = null;
    try{
     in = new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));
    }catch(IOException e){
        e.printStackTrace();
    }
    try{
        obj = (Serializable_test) in.readObject();
        in.close();
    }catch(EOFException e){
        System.out.println("END of object files reached");
    }catch(IOException e){
        e.printStackTrace();
    }catch(ClassNotFoundException e){
        e.printStackTrace();
    }


}

The first method works fine… the second method is supposed to assign the read object to the passed reference but it does not work at all (the state of the passed object remains the same as the initialized state whereas its supposed to be that of the read object) is there something wrong in the code?.

  • 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-23T01:09:23+00:00Added an answer on May 23, 2026 at 1:09 am

    This is because in Java references are passed by value. Your second method receives a copy of the reference to your object and hence updating that reference in the second method doesn’t affect the original object.

    The simplest solution would be to return the object read from the serialized stream. If you really want to update an existing object with the de-serialized object, provide an mutator update method which sets the fields of the passed in object to that of the newly read object.

    public static void deSerialize_object(Serializable_test obj, String filename) {
        File a = new File(filename);     
        ObjectInputStream in = null;
        try {
            ObjectInputStream in =
                new ObjectInputStream(new BufferedInputStream(new FileInputStream(a)));
            Serializable_test newObj = (Serializable_test) in.readObject();
            obj.update(newObject);
        } catch(Exception e) {
            e.printStackTrace();
        } finally {
            if (in != null) in.close();
        }
    }
    

    Also make sure you close your resources in the finally block.

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

Sidebar

Related Questions

Have you actually tried (means programmed in, not just read an article on it)
I just downloaded the Oxygene free command line compiler, and tried to read the
I just read Emacs :TODO indicator at left side , and tried it out.
I have just tried adding a WebService (asmx based) from a WebForms project to
I am trying to read some words from an online text file. I tried
Just tried some small graphics application of mine on Windows 7, and I'm getting
I just tried to rebase a very old branch with a minor modification onto
I just tried the tooltip like this Easy image preview ... But i cant
I just tried to Make a solution on VS 2010 to practice consuming REST
I just tried to run my company's app in the new x86 android emulator,

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.