JavaI/O文件读写:对象输出流
对象输出流 ObjectOutputStream
将对象信息写入本地文件
pojo
package cn.xtnotes.pojo;
import java.io.Serializable;
public class Stu implements Serializable{
public String name;
public int age;
public String lo;
public Stu(String name, int age, String lo) {
this.name = name;
this.age = age;
this.lo = lo;
}
}
main
package IO;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.util.ArrayList;
import cn.xtnotes.pojo.Stu;
public class PIs {
public static void main(String[] args) {
Stu stu1=new Stu("张1",15,"吃饭");
Stu stu2=new Stu("张2",15,"吃饭");
Stu stu3=new Stu("张3",15,"吃饭");
Stu stu4=new Stu("张4",15,"吃饭");
ArrayList<Stu> al=new ArrayList<Stu>();
al.add(stu1);
al.add(stu2);
al.add(stu3);
al.add(stu4);
ObjectOutputStream oss=null;
try {
oss=new ObjectOutputStream(new FileOutputStream("C:\\t\\1.txt"));
oss.writeObject(al);
oss.flush();
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally {
try {
oss.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
}
对象写入文件为乱码,可通过下节课对象输入流, 判断该文件输入后内容是否正确
阅读剩余
版权声明:
作者:Tin
链接:http://www.tinstu.com/401.html
文章版权归作者所有,未经允许请勿转载。
THE END