当前位置:网站首页>FileInputStream和BufferedInputStream的比较
FileInputStream和BufferedInputStream的比较
2022-07-06 09:19:00 【[email protected]】
InputStream是Java标准库提供的最基本的输入流,位于java.io包里,InputStream是一个抽象类,是所有输入流的超类
FileInputStream是InputStream的子类,就是从文件流中读取数据。FileInputStream是读取一个文件来作InputStream。
BufferedInputStream是缓存输入流。继承于FilterInputStream。作用是为另一个输入流添加一些功能。BufferedInputStream是套在某个其他的InputStream外,起着缓存的功能,用来改善里面那个InputStream的性能(如果可能的话),作用是为另一个输入流添加一些功能。它自己不能脱离里面那个单独存在,所以可以把BufferedInputStream套在FileInputStream外,来改善FileInputStream的性能。
区别就是:
FileInputStream是字节流,BufferedInputStream是字节缓冲流,使用BufferedInputStream读资源比FileInputStream读取资源的效率高,FileInputStream的read()方法会出现阻塞;
就是说,当缓冲区的大小比8192小的时候,BufferedInputStream的效率更好。
BufferedInputStream有一个默认为8192字节的缓冲区,当自定义的缓冲区小于8192的时候,默认一次性从硬盘中读取8192字节到内存中,然后每次只按自定义的缓冲量返回数据,性能好在了减少了读取硬盘的次数。
以下代码会更好的帮大家理解。
代码实现:
FileInputStream read()方法实现:
public class Demo05 {
public static void main(String[] args) throws IOException {
//InputStream字节输入流父类
//特点:读取字节内容的输入流
FileInputStream fis = new FileInputStream("E:\\KuGou\\test02.txt");
//逐个字节读取:每次调用read()方法,都会进行一次磁盘的读取,返回一个int类型的字节值
int data = -1;
while((data=fis.read())!=-1) {
System.out.println(data);
}
System.out.println("-----------");
//方式二
//批量读取(自建缓冲区):每次调用read(缓冲区字节数组)方法,都会进行一次磁盘的读取,返回本次
//并将读取到的内容,填充至自建缓冲区
//读取至文件末尾,返回-1
byte[] buff = new byte[4096];//字节数组代表一个缓冲区
int len = -1;
while((len=fis.read(buff))!= -1) {
System.out.println(Arrays.toString(buff));
}
}
}
BufferedInputStream read()方法代码实现
public class Demo05 {
public static void main(String[] args) throws IOException {
//InputStream字节输入流父类
//BufferedInputStream时FilterInputStream包装(装饰)器的子类
//特点:自带缓存区(默认8192个字节),必须配合FileInputStream类进行工作
BufferedInputStream bos = new BufferedInputStream(new FileInputStream("E:\\壁纸总和\\壁纸\\杨洋\\young.jpg"));
//读取
//方式一:逐个字节读取,每调用一次read()方法,都会到BufferedInputStream的缓存区中读取一个字节
//缓冲区内容读取完毕后,会一次性fill满
//bos.read();
//方式二:(推荐)
//批量读取(自建缓存区):
byte[] buffData =new byte[128];
int size = -1;
while((size = bos.read(buffData))!=-1) {
System.out.println(Arrays.toString(buffData));
}
}
}
版权声明
本文为[[email protected]]所创,转载请带上原文链接,感谢
https://blog.csdn.net/qq_49194330/article/details/124831647
边栏推荐
- 分支语句和循环语句
- Tyut Taiyuan University of technology 2022 introduction to software engineering summary
- Role movement in the first person perspective
- TYUT太原理工大学2022数据库题库选择题总结
- String class
- Application architecture of large live broadcast platform
- Iterable、Collection、List 的常见方法签名以及含义
- TYUT太原理工大学2022数据库考试题型大纲
- Fairygui bar subfamily (scroll bar, slider, progress bar)
- TYUT太原理工大学2022“mao gai”必背
猜你喜欢
如何保障 MySQL 和 Redis 的数据一致性?
2-year experience summary, tell you how to do a good job in project management
TYUT太原理工大学2022“mao gai”必背
Ten minutes to thoroughly master cache breakdown, cache penetration, cache avalanche
View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
TYUT太原理工大学2022软工导论大题汇总
[algorithm] sword finger offer2 golang interview question 13: sum of numbers of two-dimensional submatrix
MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
Smart classroom solution and mobile teaching concept description
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
随机推荐
[rtklib] preliminary practice of using robust adaptive Kalman filter under RTK
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology
TYUT太原理工大学2022软工导论大题汇总
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
系统设计学习(一)Design Pastebin.com (or Bit.ly)
Code example of MATLAB reading GNSS observation value o file
[algorithm] sword finger offer2 golang interview question 1: integer division
C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
[algorithm] sword finger offer2 golang interview question 3: the number of 1 in the binary form of the first n numbers
13 power map
Record: solution of 404 error of servlet accessing database in dynamic web project
几道高频的JVM面试题
[rtklib 2.4.3 B34] version update introduction I
Alibaba cloud microservices (IV) service mesh overview and instance istio
图书管理系统小练习
Pride-pppar source code analysis
[GNSS] robust estimation (robust estimation) principle and program implementation
闇の連鎖(LCA+树上差分)
View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
Novatel board oem617d configuration step record