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

  • Home
  • SEARCH
  • 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 6789549
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T17:37:03+00:00 2026-05-26T17:37:03+00:00

This was my first time ever trying to save object in a file, so

  • 0

This was my first time ever trying to save object in a file, so I have no idea where I’m going wrong. This is just a test program, the original one is much larger. Save is successful, backup file is created. However I can’t seem to recall that file/object. Compiling works though. Could someone please explain where exactly did I go wrong. And in a little bit more ‘beginner tutorial’ style please, I’m really bad with ‘Serializable’

import java.io.*;
import java.util.*;

class save {

    public static void main(String[] args) {

        HashMap<String, Person> list = new HashMap<String, Person>();

        Person person = new Person("12", "AAA", "XXX");
        list.put(person.getID(), person);

        if( list.containsKey(person.getID()))
            System.out.println(list.get(person.getID()));


        savePerson(person);

        list.remove(person.getID());

        if( list.containsKey(person.getID()))
            System.out.println(list.get(person.getID()));
        else
            System.out.println("Person is not available");

        person = loadPerson("12");

        System.out.println(list.size());

    }

    protected static void savePerson(Person person) {
        File source = new File("person"+person.getID()+".data");
        try { source.createNewFile(); } catch(IOException e) {System.out.println("Can't create new file : " +e.getMessage());}
        try {
            FileOutputStream personFile = new FileOutputStream("person"+person.getID()+".data");
            try {
                ObjectOutputStream personObj = new ObjectOutputStream (personFile);
                personObj.writeObject(person);
                personObj.close();
                personFile.close();
            } catch(IOException e){System.out.println("Can't save the object :" +e.getMessage());}

        } catch(FileNotFoundException e){System.out.println("Can't read the damn file :" +e.getMessage());}

    }

    protected static Person loadPerson(String ID) {
        Person person = null;
        try {
            FileInputStream personFile = new FileInputStream("person"+ID+".data");
            try {
                ObjectInputStream personObj = new ObjectInputStream(personFile);
                try {
                    person = (Person)personObj.readObject();
                    personObj.close();
                    personFile.close();
                } catch(ClassNotFoundException e){System.out.println("Can't find the class :" +e.getMessage());}
            } catch(IOException e){System.out.println("Can't save the object :" +e.getMessage());}
        } catch(FileNotFoundException e){System.out.println("Can't read the damn file :" +e.getMessage());}
        return person;
    }
}

EDIT: Here’s the person class on the request:

import java.io.*;

class Person implements Serializable {

    private String id;
    private String fname;
    private String lname;

    public Person(String id, String fname, String lname) {
        this.id = id;
        this.fname = fname;
        this.lname = lname;
    }

    protected String getID() { return id; }
    protected String getFname() { return fname; }
    protected String getLname() { return lname; }

    protected void setFname(String newFname) { fname = newFname; }

    public String toString() {
        return id + ", " + fname + " " + lname;
    }
}
  • 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-26T17:37:04+00:00Added an answer on May 26, 2026 at 5:37 pm

    Close the file after writing to it. You probably don’t get access to the file. Also, if you will fill the empty catch blocks with print of the exception, you’ll be closer to find the problem.

    You can close it by calling personFile.close() (and the same after reading the object)

    EDIT: I tested your (new) code, and it works just fine. I was able to read the object, but your code does nothing with it.

    BTW, you don’t need to close both stream, as stated in close:

    If this stream has an associated channel then the channel is closed as
    well.

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

Sidebar

Related Questions

I have recently started learning F#, and this is the first time I've ever
This is the first time I have been faced with someone trying to penetrate
This is my first time trying to package my libgdx project into .apk file
This is the first time ever I'm using AJAX, and I want to do
This is my first time with Web services. I have to develop web services
this is my first time posting here, I have a question which I have
This is the first time I'm trying random numbers with C (I miss C#).
This is my first time making a hash table. I'm trying to associate strings
This is the first time I've ever tried to use JAXB for anything at
I have used maps many times before, but this is the first time I

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.