Java对象流

来源:互联网 发布:mac播放flv 编辑:程序博客网 时间:2024/05/22 03:11

使用Java对象流写入对象信息,然后读取文件中的对象信息:

首先是一个Student类:

import java.io.Serializable;

public class Student implements Serializable {
//学生类
private static final long serialVersionUID = 1L;

private int id;
private String name;
private int age;
public Student(){

}
public Student(int id, String name, int age) {
super();
this.id = id;
this.name = name;
this.age = age;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
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;
}
@Override
public String toString() {
return "Student [id=" + id + ", name=" + name + ", age=" + age + "]";
}

}



再来是对象流的使用:

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 ObjectStream {
//对象流(传入单个对象)
public static final String PATH="student.txt";

public static void main(String[] args) {
ObjectStream os=new ObjectStream();
//创建学生对象
Student stu=new Student(1,"张三",18);
//调用写和读方法
os.writer(stu);
os.reader();
}
//往文件中写入对象
public void writer(Student stu){
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream(PATH));
oos.writeObject(stu);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(oos!=null)
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
//读取文件中的对象信息
public void reader(){
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream(PATH));
//打印输出学生对象信息
System.out.println(ois.readObject().toString());
} catch (IOException |ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ois != null)
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


}


这样我们就把对象信息写入了文件中,同时也能读出文件中的对象信息:




那么怎么传入多个对象的信息呢,可以用到对象数组,把含有多个对象的数组作为参数传入就可以了:

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 ObjectStream {
//对象流(传入对象数组)
public static final String PATH="student.txt";

public static void main(String[] args) {
ObjectStream os=new ObjectStream();
//创建5个学生对象
Student stu1=new Student(1,"张三",18);
Student stu2=new Student(2,"李四",20);
Student stu3=new Student(3,"王五",19);
Student stu4=new Student(4,"赵六",19);
Student stu5=new Student(5,"田七",17);
//存入学生对象数组中
Student[] stu={stu1,stu2,stu3,stu4,stu5};
//调用写和读方法
os.writer(stu);
os.reader();
}
//传入对象数组
public void writer(Student[] stu){
ObjectOutputStream oos=null;
try {
oos=new ObjectOutputStream(new FileOutputStream(PATH));
oos.writeObject(stu);

} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
if(oos!=null)
try {
oos.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}

}
}
public void reader(){
ObjectInputStream ois=null;
try {
ois=new ObjectInputStream(new FileInputStream(PATH));
//将readObject方法返回的Objectd对象强转为学生对象存入数组
Student[] stu1=(Student[]) ois.readObject();
//打印输出所有学生对象信息
for (Student s : stu1) {
System.out.println(s.toString());
}
} catch (IOException | ClassNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
if (ois != null)
try {
ois.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}

}


}


运行结果:



0 0
原创粉丝点击