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 + "]";
}
}