Last active
August 15, 2017 14:36
-
-
Save sekky0905/c7e72e72b1814b84e130d820fb1f826b to your computer and use it in GitHub Desktop.
Javaのシリアライズ(直列化)メモ ref: http://qiita.com/Sekky0905/items/b3c6776d10f183d8fc89
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
デシリアライズされた、Languageの名前はJavaタイプはStatic |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.Serializable; | |
/** | |
* プログラミング言語を表す | |
* Serializableインターフェースを実装する | |
*/ | |
public class Language implements Serializable { | |
/** | |
* シリアルバージョンUID | |
*/ | |
private static final long serialVersionUID = 1L; | |
/** | |
* 言語の名前 | |
*/ | |
private String name; | |
/** | |
* 言語のタイプ | |
*/ | |
private String type; | |
Language(String name, String type) { | |
this.name = name; | |
this.type = type; | |
} | |
/** | |
* 名前 | |
* | |
* @return nam 言語の名前 | |
*/ | |
public String getName() { | |
return name; | |
} | |
/** | |
* typeのゲッター | |
* | |
* @return type 言語のタイプ | |
*/ | |
public String getType() { | |
return type; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.company; | |
import java.io.*; | |
/** | |
* メインクラス | |
*/ | |
public class Main { | |
/** | |
* メインメソッド | |
* | |
* @param args コマンドライン引数 | |
*/ | |
public static void main(String args[]) { | |
String fileName = "lang.txt"; | |
// Languageクラスをインスタンス化 | |
Language language = new Language("Java", "Static"); | |
try { | |
Main.Serialize(language, fileName); | |
} catch (IOException e) { | |
e.printStackTrace(); | |
} | |
try { | |
Language deSirializedLanguage = Main.DeSerialize(fileName); | |
} catch (IOException | ClassNotFoundException e) { | |
e.printStackTrace(); | |
} | |
System.out.println("デシリアライズされた、Languageの名前は" + language.getName() + "タイプは" + language.getType()); | |
} | |
/** | |
* オブジェクトのシリアライズを行う | |
* | |
* @param language Languageクラスのインスタンス | |
* @param fileName ファイル名 | |
* @throws IOException IO例外 | |
*/ | |
private static void Serialize(Language language, String fileName) throws IOException { | |
// わかりやすくするために冗長に書いている | |
// ファイルを指定して、FileOutputStreamを生成 | |
FileOutputStream fileOutputStream = new FileOutputStream(fileName); | |
// OutputStreamに基本データ型とオブジェクトのグラフをOutputStreamに書き込む | |
// 今回は、FileOutputStreamに書き込む | |
ObjectOutputStream objectOutputStream = new ObjectOutputStream(fileOutputStream); | |
// 特定のクラスのオブジェクトの状態をストリームに書き込む | |
objectOutputStream.writeObject(language); | |
objectOutputStream.flush(); | |
objectOutputStream.close(); | |
} | |
/** | |
* オブジェクトのデシリアライズを行う | |
* | |
* @return Languageクラスのインスタンス | |
*/ | |
private static Language DeSerialize(String fileName) throws IOException, ClassNotFoundException { | |
// わかりやすくするために冗長に書いている | |
// ファイルを指定して、FileInputStreamを生成 | |
FileInputStream fileInputStream = new FileInputStream(fileName); | |
// 事前にObjectOutputStreamを使って作成されたプリミティブ・データとプリミティブ・オブジェクトを直列化復元する | |
ObjectInputStream objectInputStream = new ObjectInputStream(fileInputStream); | |
// 対応するwriteObjectメソッドによってストリームに書き込まれたデータを使用する特定のクラスのオブジェクトの状態を読み込み、復元する | |
Language language = (Language) objectInputStream.readObject(); | |
objectInputStream.close(); | |
return language; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment