Java序列化与反序列化对象方法

阅读更多
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

import org.apache.commons.codec.binary.Base64;

public class Utils {
	public static  T string2Object(String objBody, Class bean) {
		if (objBody == null || objBody.length() == 0)
			return null;

		T result = null;
		ByteArrayInputStream bais = null;
		ObjectInputStream oin = null;
		try {
			bais = new ByteArrayInputStream(new Base64().decode(objBody));
			oin = new ObjectInputStream(bais);
			result = (T) oin.readObject();
		} catch (IOException e) {
		} catch (ClassNotFoundException e) {
		} finally {
			try {
				oin.close();
				bais.close();
			} catch (IOException e) {
			}
		}

		return result;
	}

	public static  String object2String(T value) {
		if (value == null)
			return null;

		String result = null;
		ByteArrayOutputStream baos = null;
		ObjectOutputStream oos = null;
		try {
			baos = new ByteArrayOutputStream(4096);
			oos = new ObjectOutputStream(baos);
			oos.writeObject(value);
			oos.flush();

			result = new String(new Base64().encode(baos.toByteArray()));
		} catch (IOException e) {
		} finally {
			try {
				oos.close();
				baos.close();
			} catch (IOException e) {
			}
		}

		return result;
	}
}

你可能感兴趣的:(java)