0626-0631韩顺平Java Buffered字节处理流 学习笔记






如何去构建
字节流

package com.hspedu.outputstream_;import java.io.*;/*** @author abner* @version 1.0*/
public class BufferedCopy02 {public static void main(String[] args) {String srcFilePath = "D:\\Users\\Pictures\\Camera Roll\\Pierre-Auguste_Renoir,_Le_Moulin_de_la_Galette.jpg";String destFilePath = "D:\\hsp.jpg";BufferedInputStream bis = null;BufferedOutputStream bos = null;try {//因为 fis 是 ips 的子类bis = new BufferedInputStream(new FileInputStream(srcFilePath));bos = new BufferedOutputStream(new FileOutputStream(destFilePath));//循环读去文件,并写入到dfpbyte[] buff = new byte[1024];int readLen = 0;//当返回-1时,文件读取完毕while ((readLen = bis.read(buff)) != -1){bos.write(buff,0,readLen);}System.out.println("文件拷贝完毕~~~");} catch (IOException e) {e.printStackTrace();}finally {//关闭流,关闭外层处理流,即可//底层会去关闭节点流try {if(bis != null){bis.close();}if(bos !=null){bos.close();}} catch (IOException e) {e.printStackTrace();}}}
}


为什么需要对象流
int num = 100
int数据保存在文件中
保存在文件中, 100
float、double
保存数据的值,还保存数据类型
新的需求
(恢复过来还是dog对象,还要保存数据类型)



序列化和反序列化
称为反序列化
类信息
保存的是值和数据类型
重新恢复成了一个dog对象
涉及到数据类型的
可序列化
能够被数据化
serializable//标记接口,没有方法
接口在哪里 标题接口里面没有任何方法
externalizable 该接口有方法需要实现
保存的过程 成为序列化

Object
处理流包装流
修饰器模式





package com.hspedu.outputstream_;import java.io.*;/*** @author abner* @version 1.0* 演示 OOS的使用,完成数据的序列化*/
public class ObjectOutStream_ {public static void main(String[] args) throws IOException {String filePath = "D:\\data.dat";//序列化后,保存的文件格式不是纯文本的,而是按照它的方式保存的ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(filePath));//存放数据,序列化到 e:\data.datoos.write(100);//int integer(实现了序列化)oos.writeBoolean(true);//做成包装类oos.writeChar('a');//都是有序列化接口的oos.writeDouble(9.5);oos.writeUTF("韩顺平教育");//string//牢记类图//保存一个dog对象oos.writeObject(new Dog("旺财",10));
//oos.close();System.out.println("数据保存完毕(序列化形式)");}
}
//如果需要序列化某个类的对象
class Dog implements Serializable {private String name;private int age;public Dog(String name, int age) {this.name = name;this.age = age;}
}
以上是存储代码,下面是逆向输出

package com.hspedu.inputstream;import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;/*** @author abner* @version 1.0*/
public class ObjectOutStream_ {public static void main(String[] args) throws IOException, ClassNotFoundException {String filePath = "D:\\data.dat";ObjectInputStream ois = new ObjectInputStream(new FileInputStream(filePath));//读取//顺序要保持一致//否则会出现异常System.out.println(ois.readInt());System.out.println(ois.readBoolean());System.out.println(ois.readChar());System.out.println(ois.readDouble());System.out.println(ois.readUTF());// System.out.println(ois.readObject());Object o = ois.readObject();System.out.println("运行类型="+ o.getClass());System.out.println("dog信息="+o);//这里是特别重要的细节//要tostring//关闭外层流ois.close();//底层转型}
}

631
节点流和处理流
>注意事项和细节说明
1)读写顺序要一致
2)要求实现序列化或反序列化对像,需要实现Serializable
3)序列化的类中建议添加SerialVersionUID,为了提高版本的兼容性
4)序列化对象时,默认将里面所有属性都进行序列化,但除了static或transient修饰的成员
5)序列化对象时,要求里面属性的类型也需要实现序列化接口
6)序列化具备可继承性,也就是如果某类已经实现了序列化,则它的所有子类也已经默认实
现了序列化
有序列号的时候,增加一些东西,它会认为只是添加一些内容,而不是新的对象
transient、static
