ObjectInputStream和ObjectOuputStream对象输入输出流

package com.study;

import java.io.*;
/**
 * 对象输入输出流,用于对对象的输入以及输出的操作。对一个对象进行操作
 * 
 * @author Administrator
 *
 */
public class ObjectStream {
	
	public static void main(String[] args) {
		try {
			Desks d= new Desks();
			d.width="宽度";
			d.height="高度";
			ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("F:\\project\\study\\WebRoot\\object_stream.txt"));
			oos.writeObject(d);
			
			ObjectInputStream ois = new ObjectInputStream(new FileInputStream("F:\\project\\study\\WebRoot\\object_stream.txt"));
		 
			System.out.println(ois.readObject());
			ois.close();
			oos.flush();
			oos.close();
			
			
			Computer[] computer ={new Computer("100","200","三星","XP"),
									new Computer("200","1100","三星1","XP"),
									new Computer("300","1200","三星2","XP"),
									new Computer("400","2200","三星3","XP"),
									new Computer("500","3200","三星4","XP"),
									new Computer("600","4200","三星5","XP"),
									new Computer("700","5200","三星6","XP")
									};
			
			ObjectOutputStream comp_oos = new ObjectOutputStream(new FileOutputStream("F:\\project\\study\\WebRoot\\computer_stream.txt"));
			for(Computer comp : computer){
				comp_oos.writeObject(comp);
			}
			
			ObjectInputStream comp_ois =new ObjectInputStream(new FileInputStream("F:\\project\\study\\WebRoot\\computer_stream.txt"));
			for(Computer comp : computer){
				System.out.println(comp_ois.readObject());
			}
		}
		catch(IOException ioe) {
			ioe.printStackTrace();
		}
		catch(ClassNotFoundException e) {
			e.printStackTrace();
		}
	}
}

/**
 * 进行对象输入输出的时候 要进行Serializable的序列化类,
 * @author Administrator
 *
 */

class Desks implements Serializable {

	String width="100";
	String height="200";
	transient int len=1111; //此成员变量打印不考虑,为默认值
	
	/**
	 * 重写了父类toString() 方法
	 */
	public String toString() {
		return this.width+"    "+this.height+"  "+this.len;
		
	}
}

class Computer implements Serializable {
	 
	private String width;
	private String height;
	private String display_hz;
	private String os_software;
	
	
	public Computer(String width,String height,String display_hz,String os_software) {
		this.width=width;
		this.height=height;
		this.display_hz=display_hz;
		this.os_software=os_software;
	}
	
	public String toString() {
		return "计算机的配置 宽度为:"+this.width +" 高度为:"+this.height+"显示器为:"+this.display_hz+"操作系统"+this.os_software;
	}
	 
}
	
 
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值