Java serialization

What is serialization?

Serialization. Output a Java object as a byte array. By serializing it, you can write it to a file.

Serializable interface

Classes that implement the Serializable interface can be serialized.

Write the object to a file

import java.io.Serializable;

public class Person implements Serializable {

    private String greeting = "hello";

    Person(String greeting) {
        this.greeting = greeting;
    }
}
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;

public class SerializeSample {
    public static void main(String[] args) {
        var person = new Person("hello");
        try {
            var objectOutputStream = new ObjectOutputStream(new FileOutputStream("person.txt"));
            objectOutputStream.writeObject(person);
            objectOutputStream.flush();
            objectOutputStream.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }
}

Recommended Posts

Java serialization
Java serialization notes
Java
Java --Serialization and Deserialization
Java
Java Object Serialization why & when
Json serialization / deserialization in Java 1.4
Java learning (0)
Studying Java ―― 3
[Java] array
Java protected
[Java] Annotation
Java array
Studying Java ―― 9
Java scratch scratch
java (constructor)
[Java] ArrayDeque
java (override)
java (method)
Java Day 2018
Java string
java (array)
Java static
java beginner 4
JAVA paid
[Java version] The story of serialization
Studying Java ―― 4
Java (set)
java shellsort
[Java] compareTo
Studying Java -5
java reflexes
java (interface)
Java memorandum
☾ Java / Collection
Java array
Studying Java ―― 1
[Java] Array
[Java] Polymorphism
Studying Java # 0
Java review
java framework
Java features
[Java] Inheritance
FastScanner Java
Java features
java beginner 3
Java memo
java (encapsulation)
Java inheritance
[Java] Overload
Java basics
Decompile Java
[Java] Annotation
java notes
java beginner
Java (add2)
JAVA (Map)
[java] interface
Java9 collection
Java basics