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;
}
}
ObjectInputStream和ObjectOuputStream对象输入输出流
最新推荐文章于 2022-07-09 15:47:30 发布