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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T07:19:00+00:00 2026-06-05T07:19:00+00:00

I try to write a keyholder, and I want to write the passwords to

  • 0

I try to write a keyholder, and I want to write the passwords to a .dat file using ObjectOutputStream, and then read them using ObjectInputStream. This is my code for writing the objects:

public void toFile()
{    
    try
    {
        ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("passwords.dat"));     
        for(int i = 0; i<this.nrOfPW; i++)
        {
            if(this.PWlist[i] instanceof longPW)
            {
                oos.writeObject((longPW)this.PWlist[i]);
            }
            else
            {
                oos.writeObject((PinPW)this.PWlist[i]);
            }   
        }
        oos.close();
    }
    catch(IOException e)
    {
        e.getStackTrace();
    }
}

This seems to work, but when I try to read the file again and put the objects in my PWlist array it says that PinPW isn’t serializable, even though PinPW implements Serializable and it’s imported. The base class of PinPW (Info) also implements Serializable and imports it. This is the code where I read the file:

public void fromFile() 
{
    try 
    {
        ObjectInputStream objIn =  new ObjectInputStream(new FileInputStream("passwords.dat"));
        while(objIn.readObject() != null)
        {
            if(this.nrOfPW == this.PWlist.length)
            {
                expand(10);
            }
            if(objIn.readObject() instanceof PinPW)
            {
                this.PWlist[this.nrOfPW] = (PinPW)objIn.readObject();
                this.nrOfPW++;
            }
            else
            {
                this.PWlist[this.nrOfPW] = (longPW)objIn.readObject();
                this.nrOfPW++;
            }
        }
        objIn.close();
    }
    catch(EOFException e)
    {
        e.getStackTrace();
    }
    catch(IOException ex)   
    {
        ex.printStackTrace();   
    }
    catch(ClassNotFoundException ex)
    {
        ex.printStackTrace();   
    }
}

The PWlist array is a Info array, and PinPW and longPW extends Info.

What do I do to fix this problem?

  • 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-05T07:19:01+00:00Added an answer on June 5, 2026 at 7:19 am

    Let’s fix the “first bug, first” …

    In this code:

    while(objIn.readObject() != null)                         // reads object, tests then *discards* it
    {
      ...
    
      if(objIn.readObject() instanceof PinPW)                 // reads object, tests then *discards* it
      {
        this.PWlist[this.nrOfPW] = (PinPW)objIn.readObject(); // conditionally read an object
        this.nrOfPW++;
      }
      else
      {
        this.PWlist[this.nrOfPW] = (longPW)objIn.readObject(); // conditionally read an object
        this.nrOfPW++;
      }
    }
    

    Each time around your loop iteration, you actually read 3 objects. The first time you read an object to check there was one in the stream, the next time you read one and determine it’s type, then discard it. Then you read a third object and cast it based on what the type of the discarded object was.

    In addition, as EJP correctly points out, the correct way to determine End of Stream for an ObjectInputStream is to catch the end of file exception.

    You want to do this instead:

     try
     {
       while (true)
       {
         final Object o = objIn.readObject();            // read the object from the stream
    
         ...
    
         if (o instanceof PinPW)
         {
           this.PWlist[this.nrOfPW] = (PinPW) o;         // cast to correct type
           this.nrOfPW++;
         }
         else
         {
           this.PWlist[this.nrOfPW] = (longPW) o;        // cast to correct type
           this.nrOfPW++;
         }
       }
     }
     catch (EOFException e)
     {
       // end of stream reached ...
       // ... close the file descriptor etc ...
     }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I try to write code but it work not the same. I want member_preinstall
I try to write inline javascript for visa state checking This is my code
I try to write and read object of class into and from binary file
try to write a composite component that allows mutltiple text inputs. I read that
I try to write a Facebook app. This app will communicate with a rfid
I try to write a code to find the nearest value But my code
I try to write some simple rules and I get this ambiguity rule: field1
I want to try write a simple kernel in C# like cosmos, just for
I try to write a simple code in GO, in which two go routine
I try to write simple application using OpenMP. Unfortunately I have problem with speedup.

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.