How to Serialize Objects with Oracle JDK

1) Create the JdkObjectSerializationSample.java file:

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

import static java.lang.System.out;

public final class JdkSample {

  public static void main(final String[] args) throws Exception {
    final File file = new File("Sample.ser");

    final ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(file));

    oos.writeObject("Hello World!");

    oos.close();

    final ObjectInputStream ois = new ObjectInputStream(new FileInputStream(file));

    out.println(ois.readObject());

    ois.close();
  }

}

2) Compile the JdkSerializationSample.java file.

3) Launch the JdkSerializationSample class.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s