用对象流把对象存到文件中,再从文件中读取出来打印。

例如把学生对象存到文件中:

1、先创建一个学生类      ***注意 用对象流时,要保存的对象所属的类必须要实现序列化(implements Serializable)

package my_test;

import java.io.Serializable;

@SuppressWarnings("serial")
public class Student implements Serializable{

	private String name;
	private int age;
	private String sex;
	
	public Student() {
		super();
	}
	public Student(String name, int age, String sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = 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、然后定义一个把对象保存到文件中的类:


package my_test;

import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.ArrayList;

public class WriteToFile {
	public void writeTofile(ArrayList<Student>	stuList){
		File f = new File("E:\\haha");
		f.mkdir();
		f = new File("E:\\haha\\test.txt");
		try {
			if(f.exists()==false){
				f.createNewFile();
			}
			FileOutputStream fos = new FileOutputStream(f);
			ObjectOutputStream oos = new ObjectOutputStream(fos);
			oos.writeObject(stuList);
			System.out.println("成功把对象保存到文件中");
			fos.close();
			oos.close();
			
		}catch (IOException e) {
			e.printStackTrace();
		}
	}
}


3、然后定义一个从文件读取对象的类:

package my_test;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.ArrayList;

public class ReadFromFile {
	@SuppressWarnings("unchecked")
	public ArrayList<Student> readFromFile() throws IOException, ClassNotFoundException{
		FileInputStream fis = new FileInputStream("E:\\haha\\test.txt");
		ObjectInputStream ois = new ObjectInputStream(fis);
		return (ArrayList<Student>)ois.readObject();
	}
}

4、最后写一个测试类:

package my_test;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;

public class TestMain {
	/**
	 * @param args
	 * @throws ClassNotFoundException 
	 * @throws IOException 
	 */
	public static void main(String[] args) throws IOException, ClassNotFoundException {
		
		WriteToFile wtf = new WriteToFile();
		ReadFromFile rff = new ReadFromFile();
		
		ArrayList<Student> stuList = new ArrayList<Student>();
		stuList.add(new Student("hehe",20,"男"));
		stuList.add(new Student("haha",20,"男"));
		stuList.add(new Student("xixi",18,"女"));
		wtf.writeTofile(stuList);//保存到文件中
		
		ArrayList<Student>	list = rff.readFromFile();//从文件中读取出来
		System.out.println();
		System.out.println("取出对象如下:");
		
		for (Iterator<Student> iterator = list.iterator(); iterator.hasNext();) {
			Student student = (Student) iterator.next();
			System.out.println("姓名:"+student.getName()+
					"\t年龄:"+student.getAge()+
					"\t性别:"+student.getSex());
		}
	}
}

最后附上打印结果:


  • 4
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值