上面的Example05.java就可以简写为:
class Example06{
public void static main(String[] args){
BuffedInputStream in = new BuffedInputStream(new FileInputStream("D:\BaiduYunGuanjia"));
BuffedOutputStream out = new BuffedOutputStream(new FileOutputStream("D:\新建文件夹");
int len = 0 ;
while((len = in.read())!= -1){
out.write(len);
}
in.close();
out.close();
}
}
public class Example11 {
public void static main(Sring[] args){
FileReader reader = new FileReader("Example04.java");
FileWriter writer = new FileWriter("copy.java");
LineNumberReader lbreader = new LineNumberReader(reader);
FileOutputStream fos = new FileOutputStream("object.txt");
ObjectOutputStream oos = new ObjectOutputStream(fos);
oos.writeObject(p);
}
}
public class Person implements Serializable {
private String name ;
private int age;
private String sex;
Person(String name, int age, String sex){
super();
this.name = name ;
this.age = age;
this.sex = sex;
}
//有必要的话还可以添加get,set方法(函数的封装)
}
反过来,从object.txt中也可以取出来这个对象,核心代码是:
FileInputStream fis = new FileInputStream("object.txt");
ObjectInputStream ois = new ObjectInputStream(fis);
Person p = ois.readObject();