现在的位置: 首页 > 综合 > 正文

浅谈对象中的成员序列化问题

2013年08月20日 ⁄ 综合 ⁄ 共 2782字 ⁄ 字号 评论关闭

       Java对象序列化时参与序列化的内容包含以下几个方面。

        第一、属性,包括基本数据类型、数组以及其他对象的应用。

        第二、类名。

        不能被序列化的内容有以下几个方面。

        第一、方法。

        第二、有static修饰的属性。

        第三、有transient修饰的属性。

        在序列化过程中不仅保留当前类对象的数据,而且递归保存对象引用的每个对象的数据。将整个对象层次写入字节流中,这也就是序列化对象的“深复制”,即复制对象本身及引用的对象本身。序列化一个对象将可能得到整个对象序列。

        在序列化过程中,由于有些属性值比较敏感(例如密码),或者有些属性值的信息量比较大,它们不需要在网络中传递或在磁盘中存储,即不需要参与序列化。对于此类属性只需要在定义时为其添加transient关键字即可,对于transien属性序列化机制会跳过而不会将其写入文件,但在读取时也不可被恢复,该属性值保持默认初始化值。

        我简单的写了个代码,如下:

        第一、定义序列化类型Picture

       

public class Picture implements Serializable {

	private static final long serialVersionUID = 1L;

	private String name;
	private String category;
	private transient String url;
	private static int width;
	private static int length;

	public String getName() {
		return name;
	}

	public void setName(String name) {
		this.name = name;
	}

	public String getCategory() {
		return category;
	}

	public void setCategory(String category) {
		this.category = category;
	}

	public String getUrl() {
		return url;
	}

	public void setUrl(String url) {
		this.url = url;
	}

	public static int getWidth() {
		return width;
	}

	public static void setWidth(int width) {
		Picture.width = width;
	}

	public static int getLength() {
		return length;
	}

	public static void setLength(int length) {
		Picture.length = length;
	}

	@Override
	public String toString() {
		return "图片名称:" + name + ", 图片类别:" + category + ", URL=" + url + ", 长=" + length + ", 宽=" + width;
	}
	
}

        第二、对Picture对象进行读写操作

       

/**
 * 在main方法中,如果同时执行write()和read(),则结果为=======图片名称:baby, 图片类别:Portraits, URL=null, 长=128, 宽=256<br/>
 * 如果先执行write(),在执行read(),则结果为=======图片名称:baby, 图片类别:Portraits, URL=null, 长=0, 宽=0
 * @author gsucbiao
 *
 * @date 2011-9-5 && 下午11:41:55
 */
public class InputAndOutputPicture {

	public static void main(String[] args) {
		InputAndOutputPicture ps = new InputAndOutputPicture();
		
		try {
//			ps.write();
			ps.read();
		} catch (Exception e) {
			e.printStackTrace();
		}
	}

	private void read() {
		FileInputStream fis = null;
		ObjectInputStream ois = null;
		try {
			fis = new FileInputStream("E://picture.txt");
			ois = new ObjectInputStream(fis);
			Picture picture = (Picture) ois.readObject();
			System.out.println("=======" + picture.toString());
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			try {
				fis.close();
				ois.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}

	private void write() {
		FileOutputStream fos = null;
		ObjectOutputStream oos = null;
		try {
			fos = new FileOutputStream("E://picture.txt");
			oos = new ObjectOutputStream(fos);
			Picture picture = new Picture();
			picture.setName("baby");
			picture.setCategory("Portraits");
			picture.setUrl("http://www.snakespirit.picture/baby.jpg");
			picture.setLength(128);
			picture.setWidth(256);
			oos.writeObject(picture);
			oos.flush();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			try {
				fos.close();
				oos.close();
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
	}
}

        通过上述测试可知,url属性被transient关键字修饰时,该属性值不会参与文件的读写操作。而被static关键字修饰的length、width也没有参与序列化过程。

抱歉!评论已关闭.