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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T09:09:11+00:00 2026-05-27T09:09:11+00:00

I have to read a binary file. I have to read two type of

  • 0

I have to read a binary file. I have to read two type of object “Company” and “Person”. Those object are represented by the two classes: Company and Person. Those classes are derived from “User” classes.
When I read this file I have two put the two object “Company” and “Person” into two different HashMap. My professor told me I have to use the keyword instanceof but I don’t understand how use it.

            try
    {
    ObjectInputStream reader=new ObjectInputStream(new FileInputStream(fname));

        while(reader.available()>0)
        {
            User obj=reader.readObject();
                            if(obj istanceOf Company)
                            ...
                else if(obj istanceOf Person)

                     }



    }
    catch(FileNotFoundException ffe)
    {
        System.err.println("Error: the file was not found!");
    }
    catch(IOException ioe)
    {
        ioe.printStackTrace();
    }

is it right?

  • 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-27T09:09:11+00:00Added an answer on May 27, 2026 at 9:09 am

    Try something like this:

    if (obj instanceof Company) {
        Company company = (Company) obj;
        companies.put(someId, company);
    } else if (obj instanceof Person) {
        Person person = (Person) obj;
        persons.put(someId, person);
    }
    

    In the above code, obj is the Object you read from the file, assuming that you don’t know its type. Also I’m assuming that you have a Map of companies called companies and a Map of persons called persons, and in either case you must have some sort of identifier (I’m calling it someId) to use as a key for storing the object in the corresponding map.

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

Sidebar

Related Questions

I have to read a series of object from a binary file. I use:
I have a C++ program that will read in data from a binary file
I have to read a binary file in a legacy format with Java. In
I am having following issue with reading binary file in C. I have read
I have binary data in a file that I can read into a byte
I need to read a large (2000x2000) matrix of binary data from a file
I have to read a binary file in python. This is first written by
I have two question : I have a data on binary file. I want
I have written some code in C to read a binary file containing complex
I have read (or perhaps heard from a colleague) that in .NET, TransactionScope can

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.