当前位置:网站首页>Comparison between FileInputStream and bufferedinputstream
Comparison between FileInputStream and bufferedinputstream
2022-07-06 13:20:00 【[email protected]】
InputStream yes Java Standard library provides the most basic input stream , be located java.io In the bag ,InputStream Is an abstract class , Is a superclass of all input streams
FileInputStream yes InputStream Subclasses of , Reading data from a file stream .FileInputStream It's reading a file to do InputStream.
BufferedInputStream Is a cached input stream . Inherited from FilterInputStream. The function is to add some functions to another input stream .BufferedInputStream It's in some other InputStream Outside , It's a caching function , To improve the one inside InputStream Performance of ( If possible ), The function is to add some functions to another input stream . It can't separate itself from the one in it , So you can put BufferedInputStream Set on FileInputStream Outside , To improve FileInputStream Performance of .
The difference is :
FileInputStream It's a byte stream ,BufferedInputStream Is a byte buffer stream , Use BufferedInputStream Reading resource ratio FileInputStream The efficiency of reading resources is high ,FileInputStream Of read() Methods will block ;
That is to say , When the buffer size ratio 8192 When I was a child ,BufferedInputStream It's more efficient .
BufferedInputStream One defaults to 8192 Byte buffer , When the customized buffer is smaller than 8192 When , By default, it is read from the hard disk at one time 8192 Bytes into memory , Then only return data according to the customized buffer amount each time , The performance is good because it reduces the number of times to read the hard disk .
The following code will better help you understand .
Code implementation :
FileInputStream read() Method realization :
public class Demo05 {
public static void main(String[] args) throws IOException {
//InputStream Byte input stream parent
// characteristic : Read the input stream of byte content
FileInputStream fis = new FileInputStream("E:\\KuGou\\test02.txt");
// Read byte by byte : Every time you call read() Method , Will read the disk once , Return to one int Byte value of type
int data = -1;
while((data=fis.read())!=-1) {
System.out.println(data);
}
System.out.println("-----------");
// Mode two
// Bulk read ( Self built buffer ): Every time you call read( Buffer byte array ) Method , Will read the disk once , Return to this
// And will read the content , Fill in the self built buffer
// Read to the end of the file , return -1
byte[] buff = new byte[4096];// The byte array represents a buffer
int len = -1;
while((len=fis.read(buff))!= -1) {
System.out.println(Arrays.toString(buff));
}
}
}BufferedInputStream read() Method code implementation
public class Demo05 {
public static void main(String[] args) throws IOException {
//InputStream Byte input stream parent
//BufferedInputStream when FilterInputStream packing ( decorate ) Subclasses of the device
// characteristic : Built in cache ( Default 8192 Bytes ), Must cooperate FileInputStream Class to work
BufferedInputStream bos = new BufferedInputStream(new FileInputStream("E:\\ Wallpaper sum \\ wallpaper \\ Yang Yang \\young.jpg"));
// Read
// Mode one : Read byte by byte , Every call read() Method , All will be here BufferedInputStream Read a byte from the cache of
// After reading the contents of the buffer , Will be one-time fill full
//bos.read();
// Mode two :( recommend )
// Bulk read ( Self built cache ):
byte[] buffData =new byte[128];
int size = -1;
while((size = bos.read(buffData))!=-1) {
System.out.println(Arrays.toString(buffData));
}
}
}
版权声明
本文为[[email protected] Yummy]所创,转载请带上原文链接,感谢
https://yzsam.com/2022/187/202207060916019897.html
边栏推荐
- Network layer 7 protocol
- 记录:初次cmd启动MySQL拒接访问之解决
- Arduino+ water level sensor +led display + buzzer alarm
- Design a key value cache to save the results of the most recent Web server queries
- Fgui project packaging and Publishing & importing unity & the way to display the UI
- View UI Plus 发布 1.3.0 版本,新增 Space、$ImagePreview 组件
- 阿里云微服务(三)Sentinel开源流控熔断降级组件
- Tyut Taiyuan University of technology 2022 introduction to software engineering
- A brief introduction to the database of tyut Taiyuan University of technology in previous years
- [GNSS data processing] Helmert variance component estimation analysis and code implementation
猜你喜欢

(超详细onenet TCP协议接入)arduino+esp8266-01s接入物联网平台,上传实时采集数据/TCP透传(以及lua脚本如何获取和编写)

凡人修仙学指针-2

MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series

Abstract classes and interfaces

arduino+DS18B20温度传感器(蜂鸣器报警)+LCD1602显示(IIC驱动)

13 power map

抽象类和接口

Rt-ppp test using rtknavi

阿里云微服务(一)服务注册中心Nacos以及REST Template和Feign Client
![[GNSS data processing] Helmert variance component estimation analysis and code implementation](/img/4e/ff0334cf9a2a37096778a8c5719a4e.jpg)
[GNSS data processing] Helmert variance component estimation analysis and code implementation
随机推荐
Voir ui plus version 1.3.1 pour améliorer l'expérience Typescript
KF UD decomposition pseudo code implementation advanced [2]
Alibaba cloud microservices (IV) service mesh overview and instance istio
IPv6 experiment
View UI plus released version 1.3.1 to enhance the experience of typescript
All in one 1405: sum and product of prime numbers
一文搞定 UDP 和 TCP 高频面试题!
Role movement in the first person perspective
Tyut Taiyuan University of technology 2022 "Mao Gai" must be recited
Tyut Taiyuan University of technology 2022 introduction to software engineering examination question outline
The overseas sales of Xiaomi mobile phones are nearly 140million, which may explain why Xiaomi ov doesn't need Hongmeng
Fundamentals of UD decomposition of KF UD decomposition [1]
阿里云微服务(四) Service Mesh综述以及实例Istio
Alibaba cloud microservices (I) service registry Nacos, rest template and feign client
TYUT太原理工大学2022“mao gai”必背
4.30动态内存分配笔记
TYUT太原理工大学2022数据库之关系代数小题
[GNSS] robust estimation (robust estimation) principle and program implementation
FileInputStream和BufferedInputStream的比较
Problems and solutions of robust estimation in rtklib single point location spp