Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
in Java by (10.2k points)

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);



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;


1 Answer

0 votes
by (46k points)


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.*;



 * 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();


        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 );


        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;




C:\samples>javac *.java

C:\samples>java ToStringSample

Encoded serialized version





Reconstituted object

SomeClass instance says: Don't worry, I'm healthy. Look, my data is i = 2147483647, s = ABCDEFGHIJKLMNOP, d = -1.0

Related questions

Browse Categories