缓冲流
引入目的
- 作用:提升流的读取、写入的速度
- 提高读写速度的原因:内部提供了一个缓冲区,默认情况下是8kb

处理流与节点流的对比图示

缓冲流涉及到的类
BufferedInputStream
BufferedOutputStream
BufferedReader
BufferedWriter
使用说明
- 当读取数据时,数据按块读入缓冲区,其后的读操作则直接访问缓冲区
- 当使用BufferedInputStream读取字节文件时,BufferedInputStream会一次性从文件中读取8192个(8Kb),存在缓冲区中,直到缓冲区装满了,才重新从文件中读取下一个8192个字节数组
- 向流中写入字节时,不会直接写到文件,先写到缓冲区中直到缓冲区写满,BufferedOutputStream才会把缓冲区中的数据一次性写到文件里。使用方法flush()可以强制将缓冲区的内容全部写入输出流
- 关闭流的顺序和打开流的顺序相反,只要关闭最外层流即可,关闭最外层流也会相应关闭内层节点流
- flush()方法的使用:手动将buffer中内容写入文件
如果是带缓冲区的流对象的close()方法,不但会关闭流,还会在关闭流之前刷新缓冲区,关闭后不能再写出
使用BufferInputStream和BufferOutputStream实现文件的复制
@Testpublic void BufferedStreamTest() throws FileNotFoundException {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {// 1.造文件File srcFile = new File("爱情与友情.jpg");File destFile = new File("爱情与友情3.jpg");// 2.造流// 2.1 造节点流FileInputStream fis = new FileInputStream((srcFile));FileOutputStream fos = new FileOutputStream(destFile);// 2.2 造缓冲流bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);// 3.复制的细节:读取、写入byte[] buffer = new byte[10];int len;while((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);// bos.flush(); // 刷新缓冲区}} catch (IOException e) {e.printStackTrace();} finally {// 4.资源关闭// 要求:先关闭外层的流,再关闭内层的流if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}// 说明:关闭外层流的同时,内层流也会自动的进行关闭。关于内层流的关闭,我们可以省略.// fos.close();// fis.close();}}
实现文件复制的方法 ```java public void copyFileWithBuffered(String srcPath, String destPath) { BufferedInputStream bis = null; BufferedOutputStream bos = null; try {
File srcFile = new File(srcPath);File destFile = new File(destPath);FileInputStream fis = new FileInputStream((srcFile));FileOutputStream fos = new FileOutputStream(destFile);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer)) != -1){bos.write(buffer,0,len);}
} catch (IOException e) {
e.printStackTrace();
} finally {
if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}
} }
@Test public void testCopyFileWithBuffered(){ long start = System.currentTimeMillis(); String srcPath = “C:\Users\Administrator\Desktop\01-视频.avi”; String destPath = “C:\Users\Administrator\Desktop\03-视频.avi”; copyFileWithBuffered(srcPath,destPath); long end = System.currentTimeMillis(); System.out.println(“复制操作花费的时间为:” + (end - start)); // 176 }
<a name="xCWlf"></a>### 使用BufferedReader和BufferedWriter实现文本文件的复制```java@Testpublic void testBufferedReaderBufferedWriter(){BufferedReader br = null;BufferedWriter bw = null;try {br = new BufferedReader(new FileReader(new File("dbcp.txt")));bw = new BufferedWriter(new FileWriter(new File("dbcp1.txt")));// 读写操作// 方式一:使用char[]数组// char[] cbuf = new char[1024];// int len;// while((len = br.read(cbuf)) != -1) {// bw.write(cbuf, 0, len);// }// bw.flush();// 方式二:使用StringString data;while((data = br.readLine()) != null){ //🔴//方法一:// bw.write(data + "\n"); // data中不包含换行符//方法二:bw.write(data); // data中不包含换行符bw.newLine(); // 🔴提供换行的操作}} catch (IOException e) {e.printStackTrace();} finally {if(bw != null){try {bw.close();} catch (IOException e) {e.printStackTrace();}}if(br != null){try {br.close();} catch (IOException e) {e.printStackTrace();}}}}
缓冲流实现复制操作
public void copyFileWithBuffered(String srcPath,String destPath) {BufferedInputStream bis = null;BufferedOutputStream bos = null;try {File srcFile = new File(srcPath);File destFile = new File(destPath);FileInputStream fis = new FileInputStream((srcFile));FileOutputStream fos = new FileOutputStream(destFile);bis = new BufferedInputStream(fis);bos = new BufferedOutputStream(fos);byte[] buffer = new byte[1024];int len;while((len = bis.read(buffer)) != -1) {bos.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {if(bos != null){try {bos.close();} catch (IOException e) {e.printStackTrace();}}if(bis != null){try {bis.close();} catch (IOException e) {e.printStackTrace();}}}}
练习
加密操作
将图片文件通过字节流读取到程序中
将图片的字节流逐一进行^5操作
将处理后的图片字节流输出
@Testpublic void test1() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("爱情与友情.jpg");fos = new FileOutputStream("爱情与友情secret.jpg");byte[] buffer = new byte[20];int len;while ((len = fis.read(buffer)) != -1) {//字节数组进行修改//错误的// for(byte b : buffer){// b = (byte) (b ^ 5);// }//正确的for (int i = 0; i < len; i++) {buffer[i] = (byte) (buffer[i] ^ 5);}fos.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
解密操作
将加密后图片文件通过字节流读取到程序中
将图片的字节流逐一进行^操作(原理:A^B^B = A)
将处理后的图片字节流输出
@Testpublic void test2() {FileInputStream fis = null;FileOutputStream fos = null;try {fis = new FileInputStream("爱情与友情secret.jpg");fos = new FileOutputStream("爱情与友情4.jpg");byte[] buffer = new byte[20];int len;while ((len = fis.read(buffer)) != -1) {for (int i = 0; i < len; i++) {buffer[i] = (byte) (buffer[i] ^ 5);}fos.write(buffer, 0, len);}} catch (IOException e) {e.printStackTrace();} finally {if (fos != null) {try {fos.close();} catch (IOException e) {e.printStackTrace();}}if (fis != null) {try {fis.close();} catch (IOException e) {e.printStackTrace();}}}}
- 统计文本字符出现次数
实现思路:
- 遍历文本每一个字符
- 字符出现的次数存在Map中
把map中的数据写入文件
@Testpublic void testWordCount() {FileReader fr = null;BufferedWriter bw = null;try {// 1.创建Map集合Map<Character, Integer> map = new HashMap<>();// 2.遍历每一个字符,每一个字符出现的次数放到map中fr = new FileReader("dbcp.txt");int c = 0;while ((c = fr.read()) != -1) {// int 还原 charchar ch = (char) c;// 判断char是否在map中第一次出现if (map.get(ch) == null) {map.put(ch, 1);} else {map.put(ch, map.get(ch) + 1);}}// 3.把map中数据存在文件count.txt// 3.1 创建Writerbw = new BufferedWriter(new FileWriter("wordCount.txt"));// 3.2 遍历map,再写入数据Set<Map.Entry<Character, Integer>> entrySet = map.entrySet();for (Map.Entry<Character, Integer> entry : entrySet) {switch (entry.getKey()) {case ' ':bw.write("空格=" + entry.getValue());break;case '\t':bw.write("tab键=" + entry.getValue());break;case '\r':bw.write("回车=" + entry.getValue());break;case '\n':bw.write("换行=" + entry.getValue());break;default:bw.write(entry.getKey() + "=" + entry.getValue());break;}bw.newLine();}} catch (IOException e) {e.printStackTrace();} finally {if (fr != null) {try {fr.close();} catch (IOException e) {e.printStackTrace();}}if (bw != null) {try {bw.close();} catch (IOException e) {e.printStackTrace();}}}}
转换流
转换流提供了在字节流和字符流之间的转换,属于字符流
- InputstreamReader 将Inputstream转换为Reader
- OutputStreamWriter 将Writer转换为OutputStream
- 字节流中的数据都是字符时,转成字符流操作更高效
- 很多时候我们使用转换流来处理文件乱码问题。实现编码和解码的功能

InputStreamReader
- InputStreamReader将一个字节输入流转换为字符输入流
解码:字节、字节数组 —->字符数组、字符串
- 构造器
**public InputStreamReader(InputStream in)****public InputStreamReader(Inputstream in,String charsetName)** 可以指定编码集
OutputStreamWriter
- OutputStreamWriter将一个字符的输出流转换为字节的输出流
编码:字符数组、字符串 —-> 字节、字节数组
- 构造器
**public OutputStreamWriter(OutputStream out)****public OutputStreamWriter(Outputstream out,String charsetName)** 可以指定编码集
// 此时处理异常的话,仍然应该使用try-catch-finally// InputStreamReader的使用,实现字节的输入流到字符的输入流的转换@Testpublic void test1() throws IOException {FileInputStream fis = new FileInputStream("dbcp.txt");// InputStreamReader isr = new InputStreamReader(fis); //使用系统默认的字符集// 参数2指明了字符集,具体使用哪个字符集,取决于文件dbcp.txt当初保存时使用的字符集InputStreamReader isr = new InputStreamReader(fis, "UTF-8");char[] cbuf = new char[20];int len;while((len = isr.read(cbuf)) != -1) {String str = new String(cbuf, 0, len);System.out.print(str);}isr.close();}// 此时处理异常的话,仍然应该使用try-catch-finally// 综合使用InputStreamReader和OutputStreamWriter@Testpublic void test2() throws Exception {// 1.造文件、造流File file1 = new File("dbcp.txt");File file2 = new File("dbcp_gbk.txt");FileInputStream fis = new FileInputStream(file1);FileOutputStream fos = new FileOutputStream(file2);InputStreamReader isr = new InputStreamReader(fis, "utf-8");OutputStreamWriter osw = new OutputStreamWriter(fos, "gbk");// 2.读写过程char[] cbuf = new char[20];int len;while((len = isr.read(cbuf)) != -1){osw.write(cbuf, 0, len);}// 3.关闭资源isr.close();osw.close();}
说明:文件编码的方式比如GBK,决定了解析时使用的字符集也只能是GBK
编码集
常见的编码表
- ASCII:美国标准信息交换码,用一个字节的7位可以表示
- ISO8859-1:拉丁码表,欧洲码表用一个字节的8位表示
- GB2312:中国的中文编码表,最多两个字节编码所有字符
- GBK:中国的中文编码表升级,融合了更多的中文文字符号。最多两个字节编码
- Unicode:国际标准码,融合了目前人类使用的所字符。为每个字符分配唯一的字符码。所有的文字都用两个字节来表示
- UTF-8:变长的编码方式,可用1-4个字节来表示一个字符

说明
- 面向传输的众多UTF(UCS Transfer Format)标准出现了,顾名思义,UTF-8就是每次8个位传输数据,而UTF-16就是每次16个位。这是为传输而设计的编码,并使编码无国界,这样就可以显示全世界上所有文化的字符了。
- Unicode只是定义了一个庞大的、全球通用的字符集,并为每个字符规定了唯确定的编号,具体存储成什么样的字节流,取决于字符编码方案。推荐的Unicode编码是UTF-8和UTF-16。
UTF-8变长编码表示
编码应用
- 编码:字符串—>字节数组
- 解码:字节数组—>字符串
- 转换流的编码应用
- 可以将字符按指定编码格式存储
- 可以对文本数据按指定编码格式来解读
- 指定编码表的动作由构造器完成
使用要求
客户端/浏览器端 <——> 后台(java,GO,Python,Node.js,php) <——> 数据库
要求前前后后使用的字符集都要统一:UTF-8
