将实现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();
}
}
}
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();
}
}
}
相关文章推荐
- EF里一对一、一对多、多对多关系的配置和级联删除
- Delphi的时间处理
- Ubuntu 12.04以上 安装搜狗输入法
- 基于MFC的时钟绘制
- Count and Say
- 抽象abstract
- 1069. The Black Hole of Numbers
- PHOTOSHOP中3D下拉菜单为灰色如何设置
- 使用ctypes调用kernel32.dll中的函数
- 腾讯马拉松 威威猫系列故事——拼车记
- 搜狐快站,一款可视化建站工具。
- pat:1002(Advanced)
- 交换两个变量的多种方法
- 包和权限
- 学习Dojo
- 前车之鉴,后事之师:以32家创业失败的公司做案例,探寻Top20创业失败缘由
- EF里Guid类型数据的自增长、时间戳和复杂类型的用法
- 黑马程序员—类加载器
- Ubuntu下搭建Hadoop开发环境
- Python的switch实现