当前位置:网站首页>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
边栏推荐
- Fundamentals of UD decomposition of KF UD decomposition [1]
- The port is occupied because the service is not shut down normally
- Inheritance and polymorphism (I)
- 阿里云微服务(四) Service Mesh综述以及实例Istio
- 几道高频的JVM面试题
- On March 15, the official version of go 1.18 was released to learn about the latest features and usage
- The earth revolves around the sun
- [dry goods] cycle slip detection of suggestions to improve the fixed rate of RTK ambiguity
- View UI Plus 发布 1.2.0 版本,新增 Image、Skeleton、Typography组件
- Implementation of Excel import and export functions
猜你喜欢

一文搞定 UDP 和 TCP 高频面试题!

C code implementation of robust estimation in rtklib's pntpos function (standard single point positioning spp)
![[algorithm] sword finger offer2 golang interview question 1: integer division](/img/e6/f17135207b3540ec58e5a9eed54220.png)
[algorithm] sword finger offer2 golang interview question 1: integer division

Application architecture of large live broadcast platform

继承和多态(上)

记录:初次cmd启动MySQL拒接访问之解决

Small exercise of library management system

2022 National Games RE1 baby_ tree
![[algorithm] sword finger offer2 golang interview question 5: maximum product of word length](/img/e0/cea31070d6365eb57013cdead4a175.png)
[algorithm] sword finger offer2 golang interview question 5: maximum product of word length

MySQL Database Constraints
随机推荐
十分鐘徹底掌握緩存擊穿、緩存穿透、緩存雪崩
2年经验总结,告诉你如何做好项目管理
Conceptual model design of the 2022 database of tyut Taiyuan University of Technology
TYUT太原理工大学2022数据库考试题型大纲
染色法判定二分图
IPv6 experiment
阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
Smart classroom solution and mobile teaching concept description
错误: 找不到符号
How to ensure data consistency between MySQL and redis?
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
记录:Navicat Premium初次无法连接数据库MySQL之解决
121 distributed interview questions and answers
记录:下一不小心写了个递归
初识C语言(上)
TYUT太原理工大学2022软工导论简答题
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
[algorithm] sword finger offer2 golang interview question 8: the shortest subarray with a sum greater than or equal to K
Problems and solutions of robust estimation in rtklib single point location spp
E-R graph to relational model of the 2022 database of tyut Taiyuan University of Technology