我是靠谱客的博主 懦弱小甜瓜,这篇文章主要介绍java缓冲流,现在分享给大家,希望可以做个参考。

缓冲流

在流管道内增加缓存的数据,让我们使用流读取的文字更加的流畅
高级流—>创建通过低级流
这里只演示其中的一种,其他使用方式都是一样的
BufferedInputStream
构建方式 使用低级流构建
基本使用与低级流的方法完全一致
read() skip() available() close()

复制代码
1
2
3
4
5
6
7
//创建缓冲流,用低级流构建 File file = new File(); //文件路径 FileInputStream fileinputstream = new FileInputStream(file); //字节型 BufferedInputStream bufferedinputstream = new BufferedInputStream(fileinputstream);//字节型缓冲 //BufferedInputStream 的使用方法和FileInputStream的方法一致 //在BufferedOutputStream写入的时候最好写flush()这个方法,在1.8版本前是需要写的,新版本做了优化可以不写

下面是我写的一个例子

复制代码
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
File file = new File("D:\java_work\TestFile\abc.txt"); FileInputStream fileInputStream = null; BufferedInputStream bufferedInputStream = null; FileOutputStream fileOutputStream = null; BufferedOutputStream bufferedOutputStream = null; try { fileInputStream = new FileInputStream(file); bufferedInputStream = new BufferedInputStream(fileInputStream); int buffers = bufferedInputStream.available(); System.out.println("大小:" + buffers); byte[] bytes = new byte[10]; int code = bufferedInputStream.read(bytes); while (code != -1) { String value = new String(bytes, 0, code); System.out.println(value); code = bufferedInputStream.read(bytes); } System.out.println("----------------------------------------"); fileOutputStream = new FileOutputStream(file, true); bufferedOutputStream = new BufferedOutputStream(fileOutputStream); bufferedOutputStream.write('2'); bufferedOutputStream.flush(); int leg = bufferedInputStream.available(); byte[] b = new byte[leg]; int codes = bufferedInputStream.read(b); String values = new String(b, 0, codes); System.out.println("写入完成" + values); } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileInputStream != null) { fileInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedInputStream != null) { bufferedInputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fileOutputStream != null) { fileOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedOutputStream != null) { bufferedOutputStream.close(); } } catch (IOException e) { e.printStackTrace(); } } System.out.println("-------------------------------------------------------------------------"); File file1 = new File("D:\java_work\TestFile\abc.txt"); FileReader fileReader = null; BufferedReader bufferedReader = null; FileWriter fileWriter = null; BufferedWriter bufferedWriter = null; try { fileReader = new FileReader(file1); bufferedReader = new BufferedReader(fileReader); fileWriter = new FileWriter(file1); bufferedWriter = new BufferedWriter(fileWriter); bufferedWriter.write("静夜思-李白n"); bufferedWriter.write( "床前明月光,n" + "疑是地上霜,n" + "举头望明月,n" + "低头思故乡。"); bufferedWriter.flush(); char[] chars = new char[5]; int count = bufferedReader.read(chars); while (count != -1) { String value = new String(chars, 0, count); System.out.print(value); count = bufferedReader.read(chars); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fileReader != null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedReader != null) { bufferedReader.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (fileWriter != null) { fileWriter.close(); } } catch (IOException e) { e.printStackTrace(); } try { if (bufferedWriter != null) { bufferedWriter.close(); } } catch (IOException e) { e.printStackTrace(); } }

运行结果如图
在这里插入图片描述
在硬盘中文件的内容如图
在这里插入图片描述

最后

以上就是懦弱小甜瓜最近收集整理的关于java缓冲流的全部内容,更多相关java缓冲流内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(49)

评论列表共有 0 条评论

立即
投稿
返回
顶部