Showing posts with label Externalizable. Show all posts
Showing posts with label Externalizable. Show all posts

Monday, 17 October 2016

Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V7)

Master Core Java with Ram N Java!

Subscribe today for in-depth, easy-to-follow Java tutorials and core concepts!

SUBSCRIBE TO RAM N JAVA

The Limitations of Standard Serializable

The standard Serializable interface is a marker interface that tells the JVM to handle serialization automatically. While convenient, it comes with key drawbacks:

  • Lack of Control: The JVM serializes all non-transient fields across the entire class hierarchy, including metadata, which consumes unnecessary bandwidth and storage.
  • All-or-Nothing Deserialization: You cannot selectively choose which fields to deserialize.
  • Performance Overhead: Calculating default serialVersionUID values across complex inheritance trees can impact runtime performance.

Externalizable: Complete Control Over Serialization

To take full command of the serialization process, Java provides the java.io.Externalizable interface. Unlike Serializable, it gives developers direct responsibility over what data gets written to and read from the stream:

  • writeExternal(ObjectOutput out): Define the exact fields and format to serialize.
  • readExternal(ObjectInput in): Restore only the required fields in the matching order.

Key Requirements for Externalizable

When implementing Externalizable in your Java classes, keep these core rules in mind:

  • Your class must provide a public no-argument (zero-arg) constructor, which the JVM invokes during deserialization before calling readExternal.
  • Superclass state is not automatically written—you must explicitly manage inheritance if required.

Watch More Related Java Tutorials

Explore more Java serialization concepts from our channel:


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=EFeP8p2XpBg&list=UUhwKlOVR041tngjerWxVccw

Click the below Image to Enlarge 
Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V7) 
Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V7) 
Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V7) 
See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V6)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    In Java, converting an object into a byte stream so that it can be saved to a file, database, or sent across a network is called Serialization. When it comes to implementation, Java gives us two choices: Serializable and Externalizable. Understanding their core differences is essential for writing efficient applications and clearing Java technical interviews.

    Key Differences: Serializable vs Externalizable

    1. Level of Control

    Serialization (Serializable): The JVM handles the process automatically. The entire object state is converted to bytes by default without manual intervention.

    Externalization (Externalizable): The programmer has complete manual control over what data gets written and read, allowing customized serialization logic.

    2. Marker Interface vs Methods

    Serializable: It is a marker interface (contains no methods). Simply implementing it signals to the JVM that your class can be serialized.

    Externalizable: It extends Serializable and defines two required methods:

    • writeExternal(ObjectOutput out)
    • readExternal(ObjectInput in)

    3. Object Creation During Deserialization

    Serializable: No constructor is executed for the serialized class during the deserialization process.

    Externalizable: A public no-argument constructor is executed first to create the object instance before restoring fields.

    4. Role of the transient Keyword

    Serializable: Variables declared with the transient keyword are excluded from default serialization.

    Externalizable: The transient keyword has no impact. The programmer explicitly writes and reads variables in writeExternal() and readExternal() regardless of whether they are marked transient.

    Quick Comparison Summary

    Feature Serializable Externalizable
    Control JVM-controlled Programmer-controlled
    Methods 0 (Marker interface) 2 (writeExternal, readExternal)
    Constructor Call None executed Public no-arg constructor required
    transient keyword Supported Ignored

    Related Tutorials from Ram N Java


    Click here to watch in Youtube : 
    https://www.youtube.com/watch?v=-LN1Mr_4SM4&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge 
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V6) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V5)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    When working with object persistence and transmission in Java, choosing the right mechanism between Serializable and Externalizable makes a significant difference in both execution performance and data control. While both interfaces allow converting Java objects into byte streams, their operational models are distinctly different.

    Key Differences Explained

    1. Interface Type and Signatures

    Serializable: Belongs to java.io and acts as a marker interface. It has no methods or fields. Implementing it gives the JVM permission to handle object serialization automatically using reflection.

    Externalizable: A child interface of Serializable containing two mandatory callback methods:

    • public void writeExternal(ObjectOutput out) throws IOException
    • public void readExternal(ObjectInput in) throws IOException, ClassNotFoundException

    2. Performance & Control

    Serializable: Easy to implement, but default serialization incurs reflection overhead and writes extra class metadata into the stream.

    Externalizable: Gives complete control to the developer. Because you choose precisely which fields to serialize, it can achieve significantly higher performance and compact byte payloads.

    3. Constructor Requirements

    Serializable: During deserialization, no constructor is executed for the serialized class itself.

    Externalizable: Deserialization requires a public no-argument constructor to instantiate the object before invoking readExternal().

    4. Impact of the transient Modifier

    Serializable: Fields marked with transient are ignored by the default serialization mechanism.

    Externalizable: The transient keyword has no impact because all field persistence logic is manually written inside the method bodies.

    Quick Reference Summary

    Criteria Serializable Externalizable
    Control Automatic (JVM default) Manual (Developer custom)
    Method Count 0 (Marker) 2 (writeExternal / readExternal)
    No-arg Constructor Not required Mandatory (Must be public)
    Performance Standard / Slower (Reflection) Fast and customizable

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=IxyUijSQ_zk&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V5) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V4)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    In Java IO, converting an object into a sequence of bytes is a fundamental capability. Java gives developers two distinct interfaces to achieve this: Serializable and Externalizable. Deciding between them comes down to whether you prefer automated simplicity or complete custom control over your object's serialized format.

    Key Differences: Serializable vs Externalizable

    1. Default vs Custom Control

    Serializable: The entire serialization and deserialization process is handled automatically by the JVM. The virtual machine determines how fields are written and retrieved.

    Externalizable: Control is shifted entirely to the developer. You define the explicit logic for writing and restoring each field, preventing unwanted metadata from being persisted.

    2. Methods Present in the Interface

    Serializable: A marker interface with zero methods. Simply implementing it marks the class as eligible for JVM default serialization.

    Externalizable: Extends Serializable and requires two explicit callback methods:

    • writeExternal(ObjectOutput out)
    • readExternal(ObjectInput in)

    3. Constructor Behavior During Deserialization

    Serializable: The constructor of the serialized class is never called during deserialization.

    Externalizable: Deserialization mandates a public no-argument constructor to instantiate the object before invoking readExternal().

    4. Use of the transient Modifier

    Serializable: Any field declared with the transient keyword is automatically excluded from default serialization.

    Externalizable: The transient keyword has no impact because field persistence is explicitly coded inside writeExternal() and readExternal().

    Quick Reference Summary

    Concept Serializable Externalizable
    Control Level Automatic (JVM managed) Custom (Developer managed)
    Methods None (Marker interface) 2 (writeExternal / readExternal)
    Constructor Needed No constructor called Public no-argument constructor required
    Transient Keyword Supported Not applicable

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=gpl_ZmY4A4Y&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V4) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Friday, 14 October 2016

    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V3)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    When persisting objects or transmitting them over a network in Java, developers often face a common question: should you use default Serialization or opt for Externalization? While both mechanisms achieve object persistence, understanding their architectural differences helps you build faster, more memory-efficient applications.

    Core Differences Between Serializable and Externalizable

    1. Default Mechanism vs Custom Implementation

    Serializable: Managed automatically by the Java Virtual Machine (JVM). The JVM takes full responsibility for writing out the object hierarchy and fields.

    Externalizable: Gives total control to the developer. You determine exactly what data gets written to the stream and restored from it.

    2. Method Requirements

    Serializable: It is a marker interface (contains no methods). Simply tagging your class tells Java it can be serialized.

    Externalizable: Extends Serializable and provides two abstract methods that you must implement:

    • writeExternal(ObjectOutput out)
    • readExternal(ObjectInput in)

    3. Constructor Execution on Deserialization

    Serializable: No constructor is invoked for the serialized class during the deserialization process.

    Externalizable: A public no-argument constructor is mandatory. The JVM executes this constructor first to instantiate the object before calling readExternal().

    4. Transient Keyword Behavior

    Serializable: Variables marked with transient are ignored by the default serialization process.

    Externalizable: The transient keyword is ignored because the programmer explicitly decides which variables to write and read.

    Quick Comparison Summary

    Feature Serializable Externalizable
    Control Level JVM Default (Automatic) Developer Controlled (Manual)
    Method Count 0 (Marker Interface) 2 (writeExternal / readExternal)
    Constructor Call None executed for the class Public no-arg constructor required
    transient Keyword Supported Ignored

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=hqfdRb7dP18&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V3) 
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V3) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization) - Playlist

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    In Java, converting an object into a byte stream can be achieved using two main interfaces from the java.io package: Serializable and Externalizable. While both allow objects to be saved or sent over a network, they work in fundamentally different ways when it comes to JVM automation, performance, and customization.

    Key Differences: Serializable vs Externalizable

    1. Default vs Manual Serialization

    Serializable: Managed entirely by the JVM's default mechanism. It automatically serializes the object's non-transient state without requiring custom code.

    Externalizable: Gives full control to the programmer. You manually define how each field is written to and read from the stream.

    2. Marker Interface vs Custom Methods

    Serializable: A marker interface with no methods. Simply implementing it marks your class as serializable.

    Externalizable: Extends Serializable and provides two mandatory methods:

    • writeExternal(ObjectOutput out)
    • readExternal(ObjectInput in)

    3. Constructor Calls During Deserialization

    Serializable: No constructor of the serialized class is invoked during deserialization.

    Externalizable: Requires a public no-argument constructor, which is executed first by the JVM before calling readExternal().

    4. Impact of the transient Modifier

    Serializable: Fields marked with transient are skipped during serialization.

    Externalizable: The transient keyword has no effect because you explicitly write and read fields inside the custom methods.

    Quick Reference Summary

    Feature Serializable Externalizable
    Control Level JVM Default (Automatic) Developer Managed (Manual)
    Methods 0 (Marker Interface) 2 (writeExternal, readExternal)
    Constructor Needed None invoked Public no-arg constructor required
    transient Keyword Supported Ignored

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=9ErelaR_u5I&list=PLmCsXDGbJHdirIE37zMHVPk5SYWmqfeZC

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V2)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    In core Java development, preserving object state or transferring data across networks requires serialization. Java provides two mechanisms to handle this: the Serializable interface and the Externalizable interface. Knowing how each works and when to pick one over the other is an essential skill for writing clean, optimized code and answering technical interview questions.

    Key Differences: Serializable vs Externalizable

    1. Default vs Custom Control

    Serializable: Managed automatically by the Java Virtual Machine (JVM). The JVM inspects the object graph using reflection and writes all non-transient state by default.

    Externalizable: Places total control in your hands. You write explicit code specifying exactly which data fields are saved and restored.

    2. Marker Interface vs Explicit Methods

    Serializable: A marker interface that contains no methods. Implementing it simply grants permission to the JVM to serialize instances of the class.

    Externalizable: Extends Serializable and requires two methods to be implemented:

    • writeExternal(ObjectOutput out)
    • readExternal(ObjectInput in)

    3. Constructor Invocation on Deserialization

    Serializable: The serialized class constructor is not executed during object reconstruction.

    Externalizable: A public no-argument constructor is strictly required. The JVM calls it to instantiate the object before executing readExternal().

    4. Role of the transient Modifier

    Serializable: Fields marked with transient are skipped automatically during serialization.

    Externalizable: The transient modifier has no effect because you explicitly control which fields are saved and restored inside your custom methods.

    Quick Reference Summary

    Feature Serializable Externalizable
    Control Automatic (JVM default) Manual (Developer custom)
    Methods 0 (Marker interface) 2 (writeExternal, readExternal)
    Constructor Needed None called Public no-arg constructor required
    transient Keyword Supported Ignored

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=0C2vhOyYp7Y&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V2) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Thursday, 13 October 2016

    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V1)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction

    In Java, converting an object into a byte stream can be achieved using two main interfaces from the java.io package: Serializable and Externalizable. While both allow objects to be saved or sent over a network, they work in fundamentally different ways when it comes to JVM automation, performance, and customization.

    Key Differences: Serializable vs Externalizable

    1. Default vs Manual Serialization

    Serializable: Managed entirely by the JVM's default mechanism. It automatically serializes the object's non-transient state without requiring custom code.

    Externalizable: Gives full control to the programmer. You manually define how each field is written to and read from the stream.

    2. Marker Interface vs Custom Methods

    Serializable: A marker interface with no methods. Simply implementing it marks your class as serializable.

    Externalizable: Extends Serializable and provides two mandatory methods:

    • writeExternal(ObjectOutput out)
    • readExternal(ObjectInput in)

    3. Constructor Calls During Deserialization

    Serializable: No constructor of the serialized class is invoked during deserialization.

    Externalizable: Requires a public no-argument constructor, which is executed first by the JVM before calling readExternal().

    4. Impact of the transient Modifier

    Serializable: Fields marked with transient are skipped during serialization.

    Externalizable: The transient keyword has no effect because you explicitly write and read fields inside the custom methods.

    Quick Reference Summary

    Feature Serializable Externalizable
    Control Level JVM Default (Automatic) Developer Managed (Manual)
    Methods 0 (Marker Interface) 2 (writeExternal, readExternal)
    Constructor Needed None invoked Public no-arg constructor required
    transient Keyword Supported Ignored

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=9ErelaR_u5I&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java serializable vs externalizable | serialization vs externalization -V1) 
    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • Wednesday, 12 October 2016

    Java Tutorial : Java IO (Java Externalizable | Java externalization | Custom Serialization)

    🚀 Enjoying Java Tutorials?

    Subscribe to Ram N Java for clean, beginner-friendly core Java tutorials and interview prep!

    🔔 Subscribe to Ram N Java

    Introduction to Externalizable

    Standard Java serialization via Serializable is automatic, but it writes extra class metadata and persists all non-transient fields by default. When you need complete control over which data members get written and read, or need optimized performance and reduced byte sizes, Java Externalization using the Externalizable interface is the ideal solution.

    How Externalization Works

    The Externalizable interface extends java.io.Serializable. Unlike default serialization, it shifts the responsibility of serializing and deserializing entirely to the programmer through two explicit methods:

    • writeExternal(ObjectOutput out): Define your custom logic here to write only the essential fields to the output stream.
    • readExternal(ObjectInput in): Restore those fields in the exact order and data types in which they were written.

    Important Rules for Externalizable

    1. Public No-Argument Constructor

    A class implementing Externalizable must have a public no-arg constructor. During deserialization, the JVM calls this constructor first to instantiate the object before invoking readExternal().

    2. Transient Modifier is Ignored

    Because you explicitly code every read and write operation inside the callback methods, the transient keyword has no impact on externalization.

    3. Performance Optimization

    By excluding JVM metadata and only streaming the specific attributes you select, externalization significantly reduces network bandwidth and storage overhead.

    Related Tutorials from Ram N Java


    Click here to watch in Youtube :
    https://www.youtube.com/watch?v=OwN-tDEa9V4&list=UUhwKlOVR041tngjerWxVccw

    Click the below Image to Enlarge
    Java Tutorial : Java IO (Java Externalizable | Java externalization | Custom Serialization) 
    Java Tutorial : Java IO (Java Externalizable | Java externalization | Custom Serialization) 
    Java Tutorial : Java IO (Java Externalizable | Java externalization | Custom Serialization) 
    Employee.java
    import java.io.Externalizable;
    import java.io.IOException;
    import java.io.ObjectInput;
    import java.io.ObjectOutput;
    
    public class Employee implements Externalizable
    {
        private int id;
        private String name;
        private int age;
    
        public Employee()
        {
    
        }
    
        public int getId()
        {
            return id;
        }
    
        public void setId(int id)
        {
            this.id = id;
        }
    
        public String getName()
        {
            return name;
        }
    
        public void setName(String name)
        {
            this.name = name;
        }
    
        public int getAge()
        {
            return age;
        }
    
        public void setAge(int age)
        {
            this.age = age;
        }
    
        @Override
        public String toString()
        {
            return "Employee [id=" + id + ", name=" + name + 
                    ", age=" + age + "]";
        }
    
        @Override
        public void readExternal(ObjectInput in) throws IOException,
                ClassNotFoundException
        {
            System.out.println("Inside readExternal");
            id = in.readInt();
            name = (String) in.readObject();
        }
    
        @Override
        public void writeExternal(ObjectOutput out) throws IOException
        {
            System.out.println("Inside writeExternal");
            out.writeInt(id);
            out.writeObject(name);
        }
    
    }
    
    ExternalizableWrite.java
    import java.io.FileNotFoundException;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    
    public class ExternalizableWrite
    {
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException, ClassNotFoundException
        {
            ExternalizableWrite externalizableWrite = new ExternalizableWrite();
            externalizableWrite.writeEmployeeObject();
        }
    
        private void writeEmployeeObject() throws FileNotFoundException,
                IOException
        {
            FileOutputStream fileOutputStream = null;
            ObjectOutputStream objectOutputStream = null;
            //Serialize
            try
            {
                fileOutputStream = new FileOutputStream("employee.ser");
                objectOutputStream = new ObjectOutputStream(fileOutputStream);
    
                Employee employee = new Employee();
                employee.setId(101);
                employee.setName("Peter");
                employee.setAge(25);
    
                System.out.println(employee);
    
                /*
                 * Write the specified object to the
                 * ObjectOutputStream.
                 */
                objectOutputStream.writeObject(employee);
                System.out
                        .println("Successfully written employee object to the file.\n");
            }
            finally
            {
    
                if (objectOutputStream != null)
                {
                    /*
                     * Closing a ObjectOutputStream will also
                     * close the OutputStream instance to which
                     * the ObjectOutputStream is writing.
                     */
                    objectOutputStream.close();
                }
            }
    
        }
    
    }
    Output
    
    
    Employee [id=101, name=Peter, age=25]
    Inside writeExternal
    Successfully written employee object to the file.
    
    employee.ser


    ExternalizableRead.java
    import java.io.FileInputStream;
    import java.io.FileNotFoundException;
    import java.io.IOException;
    import java.io.ObjectInputStream;
    
    public class ExternalizableRead
    {
    
        public static void main(String[] args) throws FileNotFoundException,
                IOException, ClassNotFoundException
        {
            ExternalizableRead externalizableRead = new ExternalizableRead();
            externalizableRead.readEmployeeObject();
        }
    
        private void readEmployeeObject() throws IOException,
                FileNotFoundException, ClassNotFoundException
        {
            FileInputStream fileInputStream = null;
            ObjectInputStream objectInputStream = null;
            //Deserialize 
            try
            {
                fileInputStream = new FileInputStream("employee.ser");
                objectInputStream = new ObjectInputStream(fileInputStream);
    
                /*
                 * Read an object from the ObjectInputStream.
                 */
                Employee employee = (Employee) objectInputStream.readObject();
                System.out.println(employee);
                System.out
                        .println("Successfully read employee object from the file.\n");
    
                System.out.println("Id  = " + employee.getId());
                System.out.println("Name = " + employee.getName());
                System.out.println("Age = " + employee.getAge());
    
            }
            finally
            {
    
                if (objectInputStream != null)
                {
                    /*
                     * Closing a ObjectInputStream will also
                     * close the InputStream instance from which
                     * the ObjectInputStream is reading.
                     */
                    objectInputStream.close();
                }
            }
    
        }
    
    }
    
    Output
    Inside readExternal
    Employee [id=101, name=Peter, age=0]
    Successfully read employee object from the file.
    
    Id  = 101
    Name = Peter
    Age = 0
    
    
    Click the below link to download the code:
    https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Externalizable_App.zip?attredirects=0&d=1

    Github Link:
    https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Externalizable_App

    Bitbucket Link:
    https://bitbucket.org/ramram43210/java/src/9f822df4c81f286daa095d86846058f3a9c16303/BasicJava/JavaIODemo_Externalizable_App/?at=master

    See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:
    Older Posts Home

    Tutorials