文章目录
缓冲流
1. 缓冲流架构体系
2. 字节缓冲流
2.1 字节缓冲流架构
- 实例
package com.str.box.BufferInputStream;
import java.io.*;
public class demo01 {
public static void main(String[] args) throws IOException {
/*
* 需求:
* 利用字节缓冲流拷贝文件
* 字节缓冲输入流的构造方法:
* public BufferedInputStream(InputStream is)
* 字节缓冲输出流的构造方法:
* public BufferedOutputstream(OutputStream os)
* */
// 1.创建缓冲流的对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.txt"));
int ch;
while ((ch = bis.read()) != -1){
bos.write(ch);
}
bos.close();
bis.close();
}
}
package com.str.box.BufferInputStream;
import java.io.*;
public class demo02 {
public static void main(String[] args) throws IOException {
/*
* 需求:
* 利用字节缓冲流拷贝文件
* 字节缓冲输入流的构造方法:
* public BufferedInputStream(InputStream is)
* 字节缓冲输出流的构造方法:
* public BufferedOutputstream(OutputStream os)
* */
// 1.创建缓冲流的对象
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\a.txt"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.txt"));
byte[] bytes = new byte[1024];
int len;
while ((len = bis.read(bytes)) != -1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
}
2.2. 字节缓冲流提高效率的原理
3. 字符缓冲流
3.1 字符缓冲流的构造方法
- 构造方法
- 特有方法
3.2 字符缓冲流提高效率的原理
4.总结
-
缓冲流有几种?
字节缓冲输入流 : BufferedInputStream
字节缓冲输出流 : BufferedOutputStream
字符缓冲输入流 : BufferedReader
字符缓冲输出流 : BufferedWriter -
缓冲流为什么能提高性能
缓冲流自带长度为8192的缓冲区
可以显著提高字节流的读写性能
对于字符流提升不明显,对于字符缓冲流而言关键点是两个特有的方法 -
字符缓冲流两个特有的方法是什么?
字符缓冲输入流:BufferedReader: readLine ()
字符缓冲输出流:BufferedWriter: newLine ()
5. 缓冲流的应用例子
5.1 综合训练1
四种方式拷贝文件,并统计各自用时(推荐用第二种和第四种)
package com.str.box.Buffer;
import java.io.*;
public class demo01 {
public static void main(String[] args) throws IOException{
long start = System.currentTimeMillis();
// method1(); // 180.447秒
// method2(); // 0.054秒
// method3(); // 0.587秒
method4(); // 0.048秒
long end = System.currentTimeMillis();
System.out.println((end-start)/1000.0+"秒");
}
// 字节缓冲流:一次读写一个字节数组
private static void method4() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\练习\\电赛\\1.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.mp4"));
byte[] bytes = new byte[8192];
int len;
while ((len = bis.read(bytes))!= -1){
bos.write(bytes,0,len);
}
bos.close();
bis.close();
}
// 字节缓冲流:一次读一个字节
private static void method3() throws IOException{
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\练习\\电赛\\1.mp4"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.mp4"));
int b;
while ((b=bis.read())!=-1){
bos.write(b);
}
bis.close();
bos.close();
}
// 字节流的基本流:一次读写一个字节数组
private static void method2() throws IOException {
FileInputStream fis = new FileInputStream("D:\\练习\\电赛\\1.mp4");
FileOutputStream fos = new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.mp4");
byte[] bytes = new byte[8192];
int len;
while ((len = fis.read(bytes))!=-1){
fos.write(bytes,0,len);
}
fos.close();
fis.close();
}
// 字节流的基本流:一次读写一个字节
private static void method1() throws IOException {
FileInputStream fis = new FileInputStream("D:\\练习\\电赛\\1.mp4");
FileOutputStream fos = new FileOutputStream("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.mp4");
int b;
while ((b = fis.read())!=-1){
fos.write(b);
}
fos.close();
fis.close();
}
}
5.2 综合训练2
修改文本顺序进行恢复到一个新的文件
- 方法一:
package com.str.box.Buffer;
import java.io.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
public class demo02 {
public static void main(String[] args) throws IOException {
/*
需求:将文章顺序进行恢复到一个新文件中
* */
// 1.读取数据(缓冲字符流:可以读完整的一行)
BufferedReader br = new BufferedReader(new FileReader("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\a.txt"));
String line;
ArrayList<String> list = new ArrayList<>();
while ((line = br.readLine()) != null){
/*System.out.println(line);*/
list.add(line);
}
br.close();
// 2.排序
// 排序的规则,按照每一行前面的序号进行排列
Collections.sort(list, new Comparator<String>() {
@Override
// 获取o1和o2的序号
public int compare(String o1, String o2) {
int i1 = Integer.parseInt(o1.split("\\.")[0]);
int i2 = Integer.parseInt(o2.split("\\.")[0]);
return i1-i2;
}
});
// 3.写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.txt"));
for (String s : list) {
bw.write(s);
bw.newLine();//记得换行!!!
}
bw.close();
}
}
- 方法2
package com.str.box.Buffer;
import java.io.*;
import java.util.*;
public class demo02Case2 {
public static void main(String[] args) throws IOException {
/*
需求:将文章顺序进行恢复到一个新文件中
* */
// 1.读取数据(缓冲字符流:可以读完整的一行)
BufferedReader br = new BufferedReader(new FileReader("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\a.txt"));
String line;
TreeMap<Integer,String> treeMap = new TreeMap<>();
while ((line = br.readLine()) != null){
String[] arr = line.split("\\.");
// treeMap.put(Integer.parseInt(arr[0]),line);
treeMap.put(Integer.parseInt(arr[0]),arr[1]);
}
br.close();
// 2.写入数据
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\copy.txt"));
Set<Map.Entry<Integer,String>> entries = treeMap.entrySet();
for (Map.Entry<Integer, String> entry : entries) {
String value = entry.getValue();
bw.write(value);
bw.newLine();
}
bw.close();
}
}
5.3 综合训练3
软件运行次数
实现一个验证程序运行次数的小程序,要求如下:
1.当程序运行超过3次时给出提示:本软件只能免费使用3次,欢迎您注册会员后继续使用~
2.程序运行演示如下:
第一次运行控制台输出: 欢迎使用本软件第1次使用免费~
第二次运行控制台输出:欢迎使用本软件第2次使用免费~
第三次运行控制台输出: 欢迎使用本软件第3次使用免费~
第四次及之后运行控制台输出:本软件只能免费使用3次,欢迎您注册会员后继续使用~
package com.str.box.Buffer;
import java.io.*;
public class demo03 {
public static void main(String[] args) throws IOException {
/*
* 次数 计数器
* int count = 0; // 内存中
* 应该把访问次数,存在本地文件或者数据库中,才能保证数据不会丢失
* */
/*IO流:随用随时创建
* 什么时候不用直接关闭*/
// 1.把文件中的数字读取到内存中
BufferedReader br = new BufferedReader(new FileReader("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\count.txt"));
String line = br.readLine();
br.close();
int count = Integer.parseInt(line);
count++;
// 2.判断 <=3 正常运行 >3 不能运行
if (count <= 3){
System.out.println("欢迎使用本软件,第"+count+"次使用免费~");
}else {
System.out.println("本软件只能免费使用"+count+"次,欢迎您注册会员后继续使用~");
}
// 3.把当前自增之后的count值写到文件中
BufferedWriter bw = new BufferedWriter(new FileWriter("D:\\笔记\\Java笔记\\JavaSE\\IO流\\ByteStreamDemo01\\count.txt"));
bw.write(count+"");// 97 a
bw.close();
}
}