将实现Serializable接口的对象存放进文件中,然后读取出来

一、1.Student类作为对象

import java.io.Serializable;

public class Student implements Serializable{

 private transient String name;//
 private int age;
 private String sex;
 
 
 
 public String getName() {
  return name;
 }
 public void setName(String name) {
  this.name = name;
 }
 public int getAge() {
  return age;
 }
 public void setAge(int age) {
  this.age = age;
 }
 public String getSex() {
  return sex;
 }
 public void setSex(String sex) {
  this.sex = sex;
 }

}

 

 

2.写入sex.ser件和读取sex.ser


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 MainActivity {
 FileOutputStream fl;
 ObjectOutputStream f;
 FileInputStream fs;
 ObjectInputStream os;

 
  

public static void main(String[] args) {
 new MainActivity().xie();
 new MainActivity().du();
}
 //读取sex.ser文件
public void du()
{
    try {
  fs=new FileInputStream("sex.ser");
  os=new ObjectInputStream(fs);
  
 // System.out.println(os.readObject());
  Student a=new Student();
    a=(Student)os.readObject();
    System.out.println(a.getName()+a.getAge()+a.getSex());
 } catch (Exception e) {
  // TODO Auto-generated catch block
  e.printStackTrace();
  //System.out.println();
 } 
}
 //写入sex.ser文件
 public void xie()
 {
  try {
   fl=new FileOutputStream("sex.ser");
   f=new ObjectOutputStream(fl);
   
   Student sd=new Student();
   sd.setName("小明");
   sd.setAge(12);
   sd.setSex("女");
   f.writeObject(sd);
   
   
  } catch (Exception e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  }finally{
      try {
       f.close();
    fl.close();
   } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
   }
  }
 }
}

3.被transient修饰过的就不能被序列化后写入文件

      运行结果:  null 12 女 

 

二、如果有多个对象序列化后被写入文件,先被写入文件的先被读出来

1.新建一个People类

import java.io.Serializable;




public class People implements Serializable{


private String id;
private int sage;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public int getSage() {
return sage;
}
public void setSage(int sage) {
this.sage = sage;
}

}

2.然后再写入\读取文件

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 num1 {


FileOutputStream fos;
ObjectOutputStream os;
FileInputStream fi;
ObjectInputStream ois;


public static void main(String[] args) {
new num1().xie();
new num1().du();
}

public void xie()
{
try {
fos=new FileOutputStream("rax.ser");
os=new ObjectOutputStream(fos);
Student s=new Student();
s.setName("小兵");
s.setAge(5);
s.setSex("男");
People p=new People();
p.setId("7");
p.setSage(8);
os.writeObject(s);
os.writeObject(p);


} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
try {
os.close();
fos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

public void du()
{
try {
fi=new FileInputStream("rax.ser");
   ois=new ObjectInputStream(fi);
 
   Student ss=(Student) ois.readObject();
   System.out.println("ss="+ss.getAge());
   People s=(People)ois.readObject();
   System.out.println("s="+s.getId());
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值