DataInputStream && ObjectInputStream 示例

DataInputStreamDemo


package com.wxh.data;

//DataInputStream 按照相同的格式先写再读
import java.io.*;
import java.util.ArrayList;
import java.util.List;

import com.utils.StreamClose;

public class DataInputStreamDemo {

	public static void main(String[] args) {
		File file = new File("E:\\test\\data.txt");// 写入目标文件路径

		int[] arr = new int[] { 10, 20, 30, 40 };

		// 把int[] 存入文件
		saveInFile(arr, file);
		String listStr = readFromFile(file).toString();
		System.out.println(readFromFile(file).size());
		System.out.println(listStr);

	}

	// 存文件
	private static void saveInFile(int[] arr, File file) {
		DataOutputStream dos = null;

		try {
			dos = new DataOutputStream(new FileOutputStream(file));
			// 开始写文件
			for (int i : arr) {
				dos.writeInt(i);
			}
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			StreamClose.outputStreamClose(dos);
		}
	}

	// 读文件
	private static List<Integer> readFromFile(File file) {
		List<Integer> list = new ArrayList<Integer>();

		DataInputStream dis = null;

		try {
			dis = new DataInputStream(new FileInputStream(file));
			while (true) {
				int a = dis.readInt();
				list.add(a);
			}
		} catch (FileNotFoundException e) {
			// e.printStackTrace();
		} catch (IOException e) {
			if (e instanceof EOFException) {
				System.out.println("读取完毕");
			} else {
				e.printStackTrace();
			}
		} finally {
			StreamClose.inputStreamClose(dis);
		}
		return list;
	}
}

ObjectInputStreamDemo

package com.wxh.data;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

import com.utils.StreamClose;

public class ObjectInputStreamDemo {

	public static void main(String[] args) {
		List<Person> personList=new ArrayList<Person>();
		
		for(int i=0;i<12;i++){
			personList.add(new Person("路易斯"+i+"代",i+20,i%2==0));
		}
		
		
		File file=new File("E:\\test\\person.txt");
		saveInFile(file,personList);
		List<Person> list=readFromFile(file);
		System.out.println(list.toString());
		
	}

	private static void saveInFile(File file, List<Person> personList) {
		ObjectOutputStream oos=null;
		try {
			oos=new ObjectOutputStream(new FileOutputStream(file));
			for(Person p:personList){
				//将对象写入文件
				oos.writeObject(p);
			}		
			
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}finally{
			StreamClose.outputStreamClose(oos);
		}		
	}
	
	
	private static List<Person> readFromFile(File file) {
		List<Person> personList = new ArrayList<Person>();

		ObjectInputStream ois = null;
		try {
			ois = new ObjectInputStream(new FileInputStream(file));

			while (true) {
				Object obj = ois.readObject();
				if (obj instanceof Person) {
					personList.add((Person) obj);
				}
			}

		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (EOFException e) {
			System.out.println("文件读取完毕");
		} catch (IOException e) {
			e.printStackTrace();
		} catch (ClassNotFoundException e) {
			e.printStackTrace();
		} finally {
			StreamClose.inputStreamClose(ois);;
		}
		return personList;
	}

	private static void StreamcloseInput(ObjectInputStream ois) {
		// TODO Auto-generated method stub
		
	}
	
}


Person

package com.wxh.data;

import java.io.Serializable;

public class Person implements Serializable{
	private String name;
	private int age;
	private boolean 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 boolean isSex() {
		return sex;
	}
	public void setSex(boolean sex) {
		this.sex = sex;
	}
	public Person(String name, int age, boolean sex) {
		super();
		this.name = name;
		this.age = age;
		this.sex = sex;
	}
	@Override
	public String toString() {
		return "Person [name=" + name + ", age=" + age + ", sex=" + sex + "]";
	}	
}





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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值