序列化
<h1>序列化</h1>
<h3>序列化步驟</h3>
<ol>
<li>创建一个类,继承Serializable 接口</li>
<li>创建对象</li>
<li>将对象写入到文件</li>
<li>从文件读取对象信息</li>
</ol>
<h3>对象读取</h3>
<pre><code>对象输入流 ObjectInputStream
对象输出流 ObjectOutputStream</code></pre>
<h2>序列化</h2>
<pre><code>把java 对象转为字节序列的过程</code></pre>
<h2>反序列化</h2>
<pre><code>把字节序列恢复为java 对象的过程</code></pre>
<pre><code class="language-java"> private static ObjectOutputStream oss;
private static ObjectInputStream oread;
public static void main(String[] args) {
String Path = "D:\\runtime\\cache\\Object.txt";
// 定义一个Goods类对象
ObjectStreamGoods goodsClass = new ObjectStreamGoods("128415D","点单",600);
try {
FileOutputStream fos = new FileOutputStream(Path);
oss = new ObjectOutputStream(fos);
FileInputStream file = new FileInputStream(Path);
oread = new ObjectInputStream(file);
oss.writeObject(goodsClass);
// oss.writeBoolean(true); # 布尔类型的写入
try {
Object goods = (ObjectStreamGoods)oread.readObject();
// boolean result = oread.readBoolean();
System.out.print(goods);
// System.out.println(result);
} catch (ClassNotFoundException e) {
e.printStackTrace();
}
oss.flush();
fos.close();
oss.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
}catch(IOException e) {
e.printStackTrace();
}
}</code></pre>