第三部分 第十章 1.缓冲流
条评论10.1 缓冲流
10.1.1 概述
缓冲流,也叫高效流,是对4个基本的FileXxx
流的增强,所以也是4个流,按照数据类型分类:
字节缓冲流:
BufferedInputStream
,BufferedOutputStream
字符缓冲流:
BufferedReader
,BufferedWriter
缓冲流的基本原理,是在创建流对象时,会创建一个内置的默认大小的缓冲区数组,通过缓冲区读写,减少系统IO次数,从而提高读写的效率。
10.1.2 字节缓冲流
构造方法
public BufferedInputStream(InputStream in)
:创建一个 新的缓冲输入流。public BufferedOutputStream(OutputStream out)
:创建一个新的缓冲输出流。
构造举例,代码如下:
1 | // 创建字节缓冲输入流 |
效率测试
查询API,缓冲流读写方法与基本的流是一致的,我们通过复制大文件(375MB),测试它的效率。
基本流,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26public class BufferedDemo {
public static void main(String[] args) throws FileNotFoundException {
// 记录开始时间
long start = System.currentTimeMillis();
// 创建流对象
try (
FileInputStream fis = new FileInputStream("jdk9.exe");
FileOutputStream fos = new FileOutputStream("copy.exe")
){
// 读写数据
int b;
while ((b = fis.read()) != ‐1) {
fos.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
fos.close();
fis.close();
// 记录结束时间
long end = System.currentTimeMillis();
System.out.println("普通流复制时间:"+(end ‐ start)+" 毫秒");
}
}
// 十几分钟过去了...缓冲流,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26public class BufferedDemo {
public static void main(String[] args) throws FileNotFoundException {
// 记录开始时间
long start = System.currentTimeMillis();
// 创建流对象
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe"));
){
// 读写数据
int b;
while ((b = bis.read()) != ‐1) {
bos.write(b);
}
} catch (IOException e) {
e.printStackTrace();
}
fos.close();
fis.close();
// 记录结束时间
long end = System.currentTimeMillis();
System.out.println("缓冲流复制时间:"+(end ‐ start)+" 毫秒");
}
}
//缓冲流复制时间:8016 毫秒如何更快呢?
使用数组的方式,代码如下:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27public class BufferedDemo {
public static void main(String[] args) throws FileNotFoundException {
// 记录开始时间
long start = System.currentTimeMillis();
// 创建流对象
try (
BufferedInputStream bis = new BufferedInputStream(new FileInputStream("jdk9.exe"));
BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream("copy.exe"));
){
// 读写数据
int len;
byte[] bytes = new byte[8*1024];
while ((len = bis.read(bytes)) != ‐1) {
bos.write(bytes, 0 , len);
}
} catch (IOException e) {
e.printStackTrace();
}
fos.close();
fis.close();
// 记录结束时间
long end = System.currentTimeMillis();
System.out.println("缓冲流使用数组复制时间:"+(end ‐ start)+" 毫秒");
}
}
//缓冲流使用数组复制时间:666 毫秒
10.1.3 字符缓冲流
构造方法
public BufferedReader(Reader in)
:创建一个 新的缓冲输入流。public BufferedWriter(Writer out)
:创建一个新的缓冲输出流。
构造举例,代码如下:
1 | // 创建字符缓冲输入流 |
特有方法
字符缓冲流的基本方法与普通字符流调用方式一致,不再阐述,我们来看它们具备的特有方法。
BufferedReader :
public String readLine()
:读一行文字。BufferedWriter :
public void newLine()
:写一行行分隔符,由系统属性定义符号。
readLine
方法演示,代码如下:
1 | public class BufferedReaderDemo { |
newLine
方法演示,代码如下:
1 | public class BufferedWriterDemo throws IOException { |
1 | 输出效果: |
10.1.4 练习:文本排序
请将文本信息恢复顺序。
1 | 3.侍中、侍郎郭攸之、费祎、董允等,此皆良实,志虑忠纯,是以先帝简拔以遗陛下。愚以为宫中之事,事无大小,悉以咨之,然后施行,必得裨补阙漏,有所广益。 |
案例分析
- 逐行读取文本信息。
- 解析文本信息到集合中。
- 遍历集合,按顺序,写出文本信息。
案例实现
1 | public class BufferedTest { |
本文标题:第三部分 第十章 1.缓冲流
文章作者:foreverSFJ
发布时间:2019-08-21 08:51:50
最后更新:2019-08-21 08:51:50
原始链接:Notes/Java/Basic/Part03/10_1 缓冲流.html
版权声明:本博客所有文章除特别声明外,均采用 CC BY-NC-ND 4.0 许可协议。转载请注明出处!
分享