缓冲流
概述
缓冲流也称为高效流,或者是高级流。之前学习的字节流可以称为原始流
作用:缓冲流自带缓冲区,可以提高原始字节流,字符流读写数据的性能
原理:
分类
字节缓冲流(BufferedInputstream,BufferedOutputStream)
字节缓冲流型性能优化原理:
字节缓冲输入流自带了8KB缓冲值,以后我们直接从缓冲池读取数据,所以性能比较好
字节缓冲输出流自带了8KB缓冲池,数据就直接写入到缓冲池中去,写数据性能就提高了
package com.itliuxue.d1_byte_buffer;
import java.io.*;
public class ByteBufferDemo {
public static void main(String[] args) {
try(
InputStream is = new FileInputStream("C:\\Users\\LIUXUE\\Desktop\\v2-e584401c34610cb853dba8b73782fc2a_r.jpg");
OutputStream os = new FileOutputStream("C:\\Users\\LIUXUE\\Desktop\\copy2.jpg");
//把原始的字节流包装成高级的缓冲字节流
InputStream bis = new BufferedInputStream(is);
OutputStream bos = new BufferedOutputStream(os);
){
byte[] buffer = new byte[1024];
int len;
while ((len = is.read(buffer)) != -1){
os.write(buffer,0,len);
}
System.out.println("复制成功了!");
}catch (Exception e){
e.printStackTrace();
}
}
}
字符缓冲流 (Bufferedreader,BufferedWriter)
字符缓冲输入流
作用:提高字符输入流读取数据的性能,除此之外多了按照行读取数据的功能
package com.itliuxue.d3_char_buffer;
import java.io.*;
public class CharBufferDemo {
public static void main(String[] args) {
try (
Reader r = new FileReader("D:\\IDEA\\io-app2\\src\\date_charbuffer");
BufferedReader br = new BufferedReader(r);
){
// int len;
// char[] buffer = new char[1024];
// while ((len = br.read(buffer)) != -1){
// String rs = new String(buffer,0,len);
// System.out.println(rs);
// }
String line;
while ((line = br.readLine())!= null){
System.out.println(line);
}
}catch (Exception e){
e.printStackTrace();
}
}
}
字符缓冲输出流
作用;提高字符输出流写取数据的性能,除此之外多了换行功能
package com.itliuxue.d3_char_buffer;
import java.io.*;
public class CharBufferDemo02 {
public static void main(String[] args) {
try(
Writer fw = new FileWriter("D:\\IDEA\\io-app2\\src\\datecharwrite");
BufferedWriter bw = new BufferedWriter(fw);
) {
bw.write('b');
bw.write('h');
bw.write(90);
bw.newLine(); //bw.write("\r\n");
bw.write(" abc我是中国人");
bw.newLine();//bw.write("\r\n");
//字符数组
char[] chars = "abc我是中国人".toCharArray();
bw.write(chars);
}catch (Exception e){
e.printStackTrace();
}
}
}
总结
案例出师表
package com.itliuxue.d3_char_buffer;
import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class CharBufferDemoTest03 {
public static void main(String[] args) {
try {
BufferedReader br = new BufferedReader(new FileReader("D:\\IDEA\\io-app2\\src\\Test"));
List<String> list = new ArrayList<>();
String line;
while ((line = br.readLine()) != null){
list.add(line);
}
System.out.println(list);
Collections.sort(list);
System.out.println(list);
} catch (Exception e) {
e.printStackTrace();
}
}
}
转化流
问题:不同编码读取乱码问题
字符输入转换流
字符输入转化流:可以将原始的字节流按照指定的编码转化为字符输入流
总结
字符输出转换流
作用:控制写出去的字符使用的编码
序列化对象
对象序列化
作用:以内存为基准,把内存中的对象存储到磁盘文件中去,称为对象序列化
使用到的流是对象字节输出流:ObjectOutput Stream
package com.itliuxue.d7_serializable;
import java.io.Serializable;
public class Student implements Serializable {
private String name;
private String loginName;
private String passWord;
private int age;
public Student() {
}
public Student(String name, String loginName, String passWord, int age) {
this.name = name;
this.loginName = loginName;
this.passWord = passWord;
this.age = age;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getLoginName() {
return loginName;
}
public void setLoginName(String loginName) {
this.loginName = loginName;
}
public String getPassWord() {
return passWord;
}
public void setPassWord(String passWord) {
this.passWord = passWord;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
@Override
public String toString() {
return "Student{" +
"name='" + name + '\'' +
", loginName='" + loginName + '\'' +
", passWord='" + passWord + '\'' +
", age=" + age +
'}';
}
}
package com.itliuxue.d7_serializable;
import javax.xml.bind.SchemaOutputResolver;
import java.io.FileOutputStream;
import java.io.ObjectOutputStream;
public class ObjectOutputStreamDemo01 {
//将内存中的对象存储到硬盘文件中
public static void main(String[] args) {
try(
ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream("D:\\IDEA\\file-io-app\\src\\obj.txt"));
){
Student s = new Student("程磊","chenglei","1314520",20);
// 调用序列化方法
oos.writeObject(s);
// 释放资源
oos.close();
System.out.println("对象序列化完成");
}catch (Exception e){
e.printStackTrace();
}
}
}
总结
对象的反序列化
作用:以内存为基准,把存储到磁盘文件的对象数据恢复成内存中的对象,称为对象反序列化
使用到的流是对象字节输出流:ObjectIntput Stream
package com.itliuxue.d7_serializable;
import javax.xml.bind.SchemaOutputResolver;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.ObjectInputStream;
//学会使用对象反序列化,使用对象字节输入流把文件中的对象数据回复到内存中的Java对象
public class ObjectInputStreamDemo01 {
public static void main(String[] args) throws Exception {
// 创建对象字节输入流管道包装低级字节输入流
ObjectInputStream ois = new ObjectInputStream(new FileInputStream("D:\\IDEA\\file-io-app\\src\\obj.txt"));
// 调用对象字节输入流的反序列方法
Student s =(Student) ois.readObject();
System.out.println(s);
}
}
注意:当部分机密文件不想序列化时
序列化的版本号与反序列化的版本号必须一致才不会出错