Object流的应用

作者在 2010-05-09 18:29:13 发布以下内容

//这里是java bean

import java.io.Serializable;

public class StudentBean implements Serializable {//一定要实现可序列化接口
 //用Serializable来创建一个

  private int number;
  private String name;
  private String password;
  public int getNumber() {
   return number;
  }
  public void setNumber(int number) {
   this.number = number;
  }
  public String getName() {
   return name;
  }
  public void setName(String name) {
   this.name = name;
  }
  public String getPassword() {
   return password;
  }
  public void setPassword(String password) {
   this.password = password;
  }
  public StudentBean(String name, int number, String password) {
   super();
   this.name = name;
   this.number = number;
   this.password = password;
  }
  public StudentBean() {
   super();
  }
  public void showInfo(){
   System.out.println("name:" + name + "number:" + number + "password:" + password);
  }

 

}

~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~``
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;

import java.io.ObjectOutputStream;

public class StudentObjectTest {
//创建并得到一个对象流
 public static void main(String[] args) {
  try {
   FileOutputStream fos=new FileOutputStream("d:/woe.src");//创建一个文件流
   ObjectOutputStream oos=new ObjectOutputStream(fos);//将文件流放入到Object流中
   oos.writeObject(new StudentBean("adc",1,"abcd"));//创建那个对象,写入到文件中
   oos.writeObject(new StudentBean("adc2",2,"abcd2"));
   //  oos.readObject();
    //StudentBean sbn2=(StudentBean) oos.readObject();
   oos.close();
   fos.close();
   FileInputStream fis=new FileInputStream("d:/woe.src");
   ObjectInputStream ois=new ObjectInputStream(fis);
   StudentBean sbn1=(StudentBean)ois.readObject();
   StudentBean sbn2=(StudentBean)ois.readObject();
   sbn1.showInfo();
   sbn2.showInfo();
   ois.close();
  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (IOException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } catch (ClassNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }

 }

}

专业文章 | 阅读 596 次
文章评论,共0条
游客请输入验证码
浏览290913次