您的位置:首页 > 编程语言 > Java开发

java对象序列化序列化例子

2010-12-22 22:31 169 查看
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;

public class UpdatePath implements Serializable {
	private final String path;

	public UpdatePath(String path) {
		this.path = path;
	}

	public static void main(String[] arg) throws Exception {

		ByteArrayOutputStream byteoutStream = new ByteArrayOutputStream();

		ObjectOutputStream out = new ObjectOutputStream(byteoutStream);

		out.writeObject(new UpdatePath("124kkkk"));
		out.flush();

		final byte[] bytes = byteoutStream.toByteArray();

		System.out.println(bytes);
		for (int i = 0; i < bytes.length; i++) {
			System.out.print(bytes[i]);
			System.out.print(" ");
		}
		out.close();

		ObjectInputStream messageReader = new ObjectInputStream(
				new ByteArrayInputStream(bytes));

		UpdatePath path = (UpdatePath) messageReader.readObject();
		System.out.println("=============================");
		System.out.println(path.path);

		messageReader.close();

	}
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: