Skip to content

Instantly share code, notes, and snippets.

@Eriarer
Created May 24, 2023 14:49
Show Gist options
  • Save Eriarer/76e6fb7dbea685749d48706c4134ecfa to your computer and use it in GitHub Desktop.
Save Eriarer/76e6fb7dbea685749d48706c4134ecfa to your computer and use it in GitHub Desktop.
This is a basic explanation about what is serialization and how to serialize and deserialize object
╔════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ ███████ ██ ██ ██████ ██ █████ ███ ██ █████ ████████ ██ ██████ ███ ██ ║
║ ██ ██ ██ ██ ██ ██ ██ ██ ████ ██ ██ ██ ██ ██ ██ ██ ████ ██ ║
║ █████ ███ ██████ ██ ███████ ██ ██ ██ ███████ ██ ██ ██ ██ ██ ██ ██ ║
║ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ██ ║
║ ███████ ██ ██ ██ ███████ ██ ██ ██ ████ ██ ██ ██ ██ ██████ ██ ████ ║
╟────────────────────────────────────────────────────────────────────────────────────────────────────╢
║ ║
║ Serialization in Java refers to the process of converting an object into a byte stream. ║
║ Which can be easily stored in a file, transmitted over a network, or saved in a database. ║
║ This allows objects to be saved and reconstructed later, effectively preserving their state. ║
║ ║
║ To serialize an object in Java, you need to implement the Serializable interface, which is a ║
║ marker interface indicating that the object can be serialized. The serialization process is ║
║ handled by the Java runtime and doesn't require explicit coding. ║
║ ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════╝
╔════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ OBJECT FOR EXAMPLE ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════╝
You can use any object
class Person implements Serializable {
private String name;
private int age;
public Person(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
public int getAge() {
return age;
}
}
╔════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ SERIALIZE ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════╝
import java.io.*;
public class SerializationExample {
public static void main(String[] args) {
// Create an object to serialize
Person person = new Person("John Doe", 30);
// Serialize the object to a file
try (FileOutputStream fileOut = new FileOutputStream("person.ser");
ObjectOutputStream out = new ObjectOutputStream(fileOut)) {
out.writeObject(person);
System.out.println("Object serialized successfully.");
} catch (IOException e) {
e.printStackTrace();
}
}
}
╔════════════════════════════════════════════════════════════════════════════════════════════════════╗
║ DESERIALIZE ║
╚════════════════════════════════════════════════════════════════════════════════════════════════════╝
import java.io.*;
public class DeserializationExample {
public static void main(String[] args) {
// Deserialize the object from the file
try (FileInputStream fileIn = new FileInputStream("person.ser");
ObjectInputStream in = new ObjectInputStream(fileIn)) {
Person person = (Person) in.readObject();
System.out.println("Object deserialized successfully.");
// Access the object's data
System.out.println("Name: " + person.getName());
System.out.println("Age: " + person.getAge());
} catch (IOException | ClassNotFoundException e) {
e.printStackTrace();
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment