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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T21:15:49+00:00 2026-06-14T21:15:49+00:00

I need serialize objects into String and deserialize. I readed sugestion on stackoverflow and

  • 0

I need serialize objects into String and deserialize.

I readed sugestion on stackoverflow and make this code:

class Data implements Serializable {
int x = 5;
int y = 3;   
}

public class Test {
public static void main(String[] args) {

    Data data = new Data();

    String out;

    try {
        // zapis
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        ObjectOutputStream oos = new ObjectOutputStream(baos);

        oos.writeObject(data);

        out = new String(baos.toByteArray());
        System.out.println(out);

        // odczyt.==========================================

        ByteArrayInputStream bais = new ByteArrayInputStream(out.getBytes());

        ObjectInputStream ois = new ObjectInputStream(bais);

        Data d = (Data) ois.readObject();

        System.out.println("d.x = " + d.x);
        System.out.println("d.y = " + d.y);

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } catch (ClassNotFoundException e) {
        e.printStackTrace();
    }

}

}

but I get error:

java.io.StreamCorruptedException: invalid stream header: EFBFBDEF
at java.io.ObjectInputStream.readStreamHeader(ObjectInputStream.java:801)
at java.io.ObjectInputStream.<init>(ObjectInputStream.java:298)
at p.Test.main(Test.java:37)

Why?
I expected:
d.x = 5
d.y = 3

how to do in good way?
Ah. I don’t want to write this object in file. I have to have it in string format.

  • 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-14T21:15:50+00:00Added an answer on June 14, 2026 at 9:15 pm

    Use
    ByteArrayInputStream bais = new ByteArrayInputStream(baos.toByteArray()); instead of
    ByteArrayInputStream bais = new ByteArrayInputStream(out.getBytes());, since the String conversion corrupts the data (because of the encoding).

    If you really need to store the result in a String, you need a safe way to store arbitrary bytes in a String. One way of doing that is to us Base64-encoding.

    A totally different approach would have been to not use the standard Java serialization for this class, but create your own Data to/from String converter.

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

Sidebar

Related Questions

I need to serialize/de-serialize some objects into/from string and transfer them as just opaque
I need to serialize ArrayList<String> arr=new ArrayList<String>(); on server and then send this array
In the following Jackson/Java code that serializes objects into JSON, I am getting this:
I need to read and serialize objects from and to XML, Apple's .plist format
I'm using Marshal.dump to serialize an array of objects, I need to get the
I have a business class which I need to serialize to xml. It has
I have a simple Java class that I need to serialize to be stored
I have a class that I need to be able to serialize to a
I need to serialize the strings from a resource file (.resx) into a JSON
I have this method to Serialize an object with any type public static string

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.