0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

シリアライズとは

直列化。Javaオブジェクトをバイト配列として出力すること。シリアライズすることによってファイルに書き出せるようになります。

Serializableインタフェース

Serializableインタフェースを実装したクラスはシリアライズ可能になります。

オブジェクトをファイルに書き出す

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

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?