您的位置:首页 > 其它

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

2014-02-25 19:33 489 查看
一、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();

}

}

}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: