Java對象序列化成字符串和反序列化

1、序列化:序列化後保存在一個字符串變量中

package com.lxh.ser.test;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;

public class SerializeTest {

	private static final String TEMP_ENCODING = "ISO-8859-1";
	private static final String DEFAULT_ENCODING = "UTF-8";

	public static void main(String[] args) throws IOException {
		List<String> list = new ArrayList<String>();
		list.add("abc");
		list.add("ABC");
		System.out.println(writeToStr(list));
	}
<span style="white-space:pre">	/**
<span style="white-space:pre">	</span> * 將序列化的字符串反序列化成對象
<span style="white-space:pre">	</span> * @param serStr 系列化的字符串
<span style="white-space:pre">	</span> * @return Object 反序列化後得到原始的對象
<span style="white-space:pre">	</span> * @throws IOException
<span style="white-space:pre">	</span> */</span>
	public static String writeToStr(Object obj) throws IOException {
		// 此類實現了一個輸出流,其中的數據被寫入一個 byte 數組。
		// 緩衝區會隨着數據的不斷寫入而自動增長。可使用 toByteArray() 和 toString() 獲取數據。
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
		// 專用於java對象序列化,將對象進行序列化
		ObjectOutputStream objectOutputStream = null;
		String serStr = null;
		try {
			objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
			objectOutputStream.writeObject(obj);
			serStr = byteArrayOutputStream.toString(TEMP_ENCODING);
			serStr = java.net.URLEncoder.encode(serStr, DEFAULT_ENCODING);
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			objectOutputStream.close();
		}
		return serStr;
	}
}


2、反序列化:將一個序列化後的字符串反序列化爲原有對象

package com.lxh.ser.test;


import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.ArrayList;
import java.util.List;


public class SerializeTest {

<span style="white-space:pre">	</span>private static final String TEMP_ENCODING = "ISO-8859-1";
<span style="white-space:pre">	</span>private static final String DEFAULT_ENCODING = "UTF-8";


<span style="white-space:pre">	</span>public static void main(String[] args) throws IOException {
<span style="white-space:pre">		</span>List<String> list = new ArrayList<String>();
<span style="white-space:pre">		</span>list.add("abc");
<span style="white-space:pre">		</span>list.add("ABC");
<span style="white-space:pre">		</span>String serStr = writeToStr(list);
<span style="white-space:pre">		</span>System.out.println(serStr);
<span style="white-space:pre">		</span>
<span style="white-space:pre">		</span>List<String> deList = (List<String>) deserializeFromStr(serStr);
<span style="white-space:pre">		</span>System.out.println(deList.get(0) + "  " + deList.get(1));
<span style="white-space:pre">	</span>}


<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 將序列化的字符串反序列化成對象
<span style="white-space:pre">	</span> * @param serStr 系列化的字符串
<span style="white-space:pre">	</span> * @return Object 反序列化後得到原始的對象
<span style="white-space:pre">	</span> * @throws IOException
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static Object deserializeFromStr(String serStr) throws IOException {
<span style="white-space:pre">		</span>ByteArrayInputStream byteArrayInputStream = null;
<span style="white-space:pre">		</span>ObjectInputStream objectInputStream = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>String deserStr = java.net.URLDecoder.decode(serStr, DEFAULT_ENCODING);
<span style="white-space:pre">			</span>byteArrayInputStream = new ByteArrayInputStream(deserStr.getBytes(TEMP_ENCODING));  
<span style="white-space:pre">			</span>objectInputStream = new ObjectInputStream(byteArrayInputStream);   
<span style="white-space:pre">			</span>return objectInputStream.readObject();
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} catch (ClassNotFoundException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} finally {
<span style="white-space:pre">			</span>objectInputStream.close();
<span style="white-space:pre">			</span>byteArrayInputStream.close();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return null;
<span style="white-space:pre">	</span>}
<span style="white-space:pre">	</span>
<span style="white-space:pre">	</span>/**
<span style="white-space:pre">	</span> * 將對象序列化成字符串
<span style="white-space:pre">	</span> * @param obj 需要進行序列化的對象
<span style="white-space:pre">	</span> * @return String 把對象序列化成字符串
<span style="white-space:pre">	</span> * @throws IOException
<span style="white-space:pre">	</span> */
<span style="white-space:pre">	</span>public static String writeToStr(Object obj) throws IOException {
<span style="white-space:pre">		</span>// 此類實現了一個輸出流,其中的數據被寫入一個 byte 數組。
<span style="white-space:pre">		</span>// 緩衝區會隨着數據的不斷寫入而自動增長。可使用 toByteArray() 和 toString() 獲取數據。
<span style="white-space:pre">		</span>ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
<span style="white-space:pre">		</span>// 專用於java對象序列化,將對象進行序列化
<span style="white-space:pre">		</span>ObjectOutputStream objectOutputStream = null;
<span style="white-space:pre">		</span>String serStr = null;
<span style="white-space:pre">		</span>try {
<span style="white-space:pre">			</span>objectOutputStream = new ObjectOutputStream(byteArrayOutputStream);
<span style="white-space:pre">			</span>objectOutputStream.writeObject(obj);
<span style="white-space:pre">			</span>serStr = byteArrayOutputStream.toString(TEMP_ENCODING);
<span style="white-space:pre">			</span>serStr = java.net.URLEncoder.encode(serStr, DEFAULT_ENCODING);
<span style="white-space:pre">		</span>} catch (IOException e) {
<span style="white-space:pre">			</span>e.printStackTrace();
<span style="white-space:pre">		</span>} finally {
<span style="white-space:pre">			</span>objectOutputStream.close();
<span style="white-space:pre">			</span>byteArrayOutputStream.close();
<span style="white-space:pre">		</span>}
<span style="white-space:pre">		</span>return serStr;
<span style="white-space:pre">	</span>}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章