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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 10, 20262026-05-10T17:28:34+00:00 2026-05-10T17:28:34+00:00

I am able to serialize an object into a file and then restore it

  • 0

I am able to serialize an object into a file and then restore it again as is shown in the next code snippet. I would like to serialize the object into a string and store into a database instead. Can anyone help me?

LinkedList<Diff_match_patch.Patch> patches = // whatever... FileOutputStream fileStream = new FileOutputStream('foo.ser'); ObjectOutputStream os = new ObjectOutputStream(fileStream); os.writeObject(patches1); os.close();  FileInputStream fileInputStream = new FileInputStream('foo.ser'); ObjectInputStream oInputStream = new ObjectInputStream(fileInputStream); Object one = oInputStream.readObject(); LinkedList<Diff_match_patch.Patch> patches3 = (LinkedList<Diff_match_patch.Patch>) one; os.close(); 
  • 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. 2026-05-10T17:28:35+00:00Added an answer on May 10, 2026 at 5:28 pm

    Sergio:

    You should use BLOB. It is pretty straighforward with JDBC.

    The problem with the second code you posted is the encoding. You should additionally encode the bytes to make sure none of them fails.

    If you still want to write it down into a String you can encode the bytes using java.util.Base64.

    Still you should use CLOB as data type because you don’t know how long the serialized data is going to be.

    Here is a sample of how to use it.

    import java.util.*; import java.io.*;  /**   * Usage sample serializing SomeClass instance   */ public class ToStringSample {      public static void main( String [] args )  throws IOException,                                                       ClassNotFoundException {         String string = toString( new SomeClass() );         System.out.println(' Encoded serialized version ' );         System.out.println( string );         SomeClass some = ( SomeClass ) fromString( string );         System.out.println( '\n\nReconstituted object');         System.out.println( some );       }      /** Read the object from Base64 string. */    private static Object fromString( String s ) throws IOException ,                                                        ClassNotFoundException {         byte [] data = Base64.getDecoder().decode( s );         ObjectInputStream ois = new ObjectInputStream(                                          new ByteArrayInputStream(  data ) );         Object o  = ois.readObject();         ois.close();         return o;    }      /** Write the object to a Base64 string. */     private static String toString( Serializable o ) throws IOException {         ByteArrayOutputStream baos = new ByteArrayOutputStream();         ObjectOutputStream oos = new ObjectOutputStream( baos );         oos.writeObject( o );         oos.close();         return Base64.getEncoder().encodeToString(baos.toByteArray());      } }  /** Test subject. A very simple class. */  class SomeClass implements Serializable {      private final static long serialVersionUID = 1; // See Nick's comment below      int i    = Integer.MAX_VALUE;     String s = 'ABCDEFGHIJKLMNOP';     Double d = new Double( -1.0 );     public String toString(){         return  'SomeClass instance says: Don't worry, '                + 'I'm healthy. Look, my data is i = ' + i                 + ', s = ' + s + ', d = ' + d;     } } 

    Output:

    C:\samples>javac *.java  C:\samples>java ToStringSample Encoded serialized version rO0ABXNyAAlTb21lQ2xhc3MAAAAAAAAAAQIAA0kAAWlMAAFkdAASTGphdmEvbGFuZy9Eb3VibGU7T AABc3QAEkxqYXZhL2xhbmcvU3RyaW5nO3hwf////3NyABBqYXZhLmxhbmcuRG91YmxlgLPCSilr+w QCAAFEAAV2YWx1ZXhyABBqYXZhLmxhbmcuTnVtYmVyhqyVHQuU4IsCAAB4cL/wAAAAAAAAdAAQQUJ DREVGR0hJSktMTU5PUA==   Reconstituted object SomeClass instance says: Don't worry, I'm healthy. Look, my data is i = 2147483647, s = ABCDEFGHIJKLMNOP, d = -1.0 

    NOTE: for Java 7 and earlier you can see the original answer here

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

Sidebar

Ask A Question

Stats

  • Questions 75k
  • Answers 75k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • added an answer Yes, they will usually be identical. However, if you're working… May 11, 2026 at 2:44 pm
  • added an answer I settled for a method which isn't quite as generic… May 11, 2026 at 2:44 pm
  • added an answer You can use the htmlentities() function. This will replace all… May 11, 2026 at 2:44 pm

Related Questions

I am using a class library which represents some of its configuration in .xml.
I'm in the process of teaching myself WPF, and I have run into a
I have a web-app-database 3 tier server setup. Web requests data from app, and
I have a simple form that is used to update an entity in my

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.