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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T01:30:42+00:00 2026-06-03T01:30:42+00:00

My Code- import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.ObjectInputStream;

  • 0

My Code-

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;

public class ObjectStreamExample {

    /**
     * @param args
     */
    public static void main(String[] args) {

        Person person = new Person();
        person.setFirstName("Abhishek");
        person.setLastName("Choudhary");
        person.setAge(25);
        person.setHouseNum(256);
        ObjectOutputStream stream = null;
        try {
            stream = new ObjectOutputStream(new FileOutputStream(new File("Serialize.txt")));
            stream.writeUTF(person.toString());
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {

            e.printStackTrace();
        }finally{
            if(stream != null)
                try {
                    stream.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }

        ObjectInputStream input = null;

        try {
            input = new ObjectInputStream(new FileInputStream(new File("Serialize.txt")));

            Person person2 = (Person) input.readObject();
            System.out.println(person2.getFirstName());
            System.out.println(person2.getLastName());
            System.out.println(person2.getAge());
            System.out.println(person2.getHouseNum());
        } catch (IOException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }finally{
            if(input != null)
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
        }


    }

}

and one Person bean file.

I am getting exception

java.io.OptionalDataException at
java.io.ObjectInputStream.readObject0(Unknown Source) at
java.io.ObjectInputStream.readObject(Unknown Source) at
com.practise.interview.nio.ObjectStreamExample.main(ObjectStreamExample.java:62)

This is getting raised because I think –

An attempt was made to read an object when the next element in the
stream is primitive data. In this case, the OptionalDataException’s
length field is set to the number of bytes of primitive data
immediately readable from the stream, and the eof field is set to
false.

But how to avoid it as I know I set a primitive value, so way to avoid.?

  • 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-03T01:30:45+00:00Added an answer on June 3, 2026 at 1:30 am

    You are writing a String and try to read a Person. That’s not how serialization works. In the context of serialization, an UTF string is considered primitive data, because it does not contain object information (class name, attributes etc.), but only the string data.

    Write out the person object itself, if you want to read a Person afterwards:

    stream.writeObject(person);
    

    Addendum: If writing a String would behave like with any other Object, you would get a ClassCastException instead, because the String could not be cast to Person. In any case, the mismatch between what you write and what you read is causing the error you got.

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

Sidebar

Related Questions

package tutorial; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.IOException; import java.io.InputStream; import com.hp.hpl.jena.ontology.OntModel;
my code: package sample; import java.io.File; import java.io.FileInputStream; import java.io.IOException; import java.io.InputStream; import com.hp.hpl.jena.query.Query;
Here I found this code: import java.awt.*; import javax.swing.*; public class FunWithPanels extends JFrame
I'm confused by the following code: import java.util.ArrayList; import java.util.LinkedList; import java.util.List; public class
Here is a simple piece of code: import java.io.*; public class Read { public
Here is my code: import javax.swing.*; import java.awt.*; public class PanelModel { public static
Here is the code: import javax.swing.*; import java.awt.event.*; import java.awt.*; public class TestGrid {
When I sbt run the following code, package com.example import java.io.ObjectInputStream import java.io.ObjectOutputStream import
I've got the following java code, which is giving the error below: import java.io.File;
Given is the following code: import java.io.Serializable; import java.util.concurrent.locks.ReentrantLock; class Data { private int

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.