当前位置:网站首页>The difference between FileInputStream and bufferedinputstream
The difference between FileInputStream and bufferedinputstream
2022-07-26 07:55:00 【Why can't I eat mango】
Java Standard library java.io.InputStream Defines the superclass of all input streams :
FileInputStream File stream input is realized ;
BufferedInputStream Use buffer to improve the efficiency of file reading ;
ByteArrayInputStream Simulate a byte stream input in memory ;
One . FileInputStream( File input stream )
FileInputStream yes InputStream Subclasses of , Is to read data from the file stream , Every read is from the hard disk , Slow read speed ;
InputStream Pass through read() Method to read the next byte of the input stream , And return the... Represented by bytes int value (0 ~ 255), If you read -1 You can't continue reading ;
InputStream Buffer read : So let's define one byte[] Array as buffer ,read() Method will read as many bytes as possible into the buffer , But it won't exceed the size of the buffer .
Read once using buffer InputStream All bytes of ( The two methods ):
(1) Read byte by byte ;(2) Bulk read .
The code implementation is as follows :
try (InputStream in = new FileInputStream("D:\\countNumber.text")) {
// The way 1: Read byte by byte
// int data = -1;
// while((data = in.read()) != -1) {
// System.out.println((char)data);
// }
// The way 2: Bulk read
byte[] buff = new byte[1000]; // Definition 1000 Byte size buffer
int size = -1;
while((size = in.read(buff)) != -1) {
System.out.println();
}
} catch (IOException e1) {
e1.printStackTrace();
}
Two . BufferedInputStream( Buffered input stream )
BufferedInputStream Is a buffered input stream ( Byte input stream with buffer ), Inherited from FilterInputStream( yes InputStream Subclasses of ), The function is to add some functions to another input stream : Provide buffer function and support “mark() Mark ”,“reset() Reset method ";
BufferedInputStream The essence is realized through an internal buffer array , It has a default size of 8192 The buffer ( Read faster ). for example , Create an input stream corresponding to BufferedInputStream after , When we go through read() When reading data from the input stream ,BufferedInputStream The data of the input stream will be filled into the buffer in batches . Every time the data in the buffer is read , The input stream fills the data buffer again ; So again and again , Until we finish reading the input stream data location .
public class BufferedInputStream extends FilterInputStream {
private static int DEFAULT_BUFFER_SIZE = 8192;
...}
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("D:\\countNumber.text"))) {
// The way 1: Read byte by byte
// int data = -1;
//while((data = bis.read()) != -1) {
// System.out.println(data);
// }
// The way 2: Bulk read
byte[] buffData = new byte[128]; // Used to store the byte array read each time
int len = -1; // The length of the byte array read each time
while((len = bis.read(buffData)) != -1) {
System.out.println(Arrays.toString(buffData));
}
} catch (IOException e) {
e.printStackTrace();
} notes : If a file is opened for reading and writing , Close it in time after completion , In order to make os Release resources (try sentence : Shortcut key :shift+alt+z, The compiler will automatically write finally And call close()), actually , The compiler is not specifically for InputStream Plus automatic shutdown , But because it realizes AutoCloseable Interface , Thanks to the implementation of this interface , Will be written automatically finally And call close();
边栏推荐
- Interview question set
- Using ordered dictionary to copy pcap files
- 什么是消息订阅和发布?
- Use of JMeter performance test to store response content to file listener
- 2021-11-09
- Summary of distributed related interview questions
- QT listview add controls and pictures
- Meta universe infrastructure: analysis of the advantages of Web 3.0 chain33
- JMeter performance test saves the results of each interface request to a file
- No valid host was found when setting up openstack to create an instance There are not enough hosts available. code:500
猜你喜欢

Installation of Baidu flying paste deep learning framework tutorial in Anaconda

Machine learning related competition website

Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合

一文掌握mysql数据库审计特点、实现方案及审计插件部署教程
![[daily question 1] 919. Complete binary tree inserter](/img/a2/2ff77ccdfb78bb1812b989342c2331.png)
[daily question 1] 919. Complete binary tree inserter

在线问题反馈模块实战(十四):实现在线答疑功能

Open source management system based on ThinkPHP

Fang Wenshan, Jay Chou's best partner, will officially announce "Hualiu yuancosmos" on July 25

Master slave database deployment

音视频学习(十)——ps流
随机推荐
A tutorial for mastering MySQL database audit characteristics, implementation scheme and audit plug-in deployment
Wrong Addition
The difference between overloading and rewriting
NLP natural language processing - Introduction to machine learning and natural language processing (3)
Enterprise private network construction and operation and maintenance
The difference between throw and throws?
1.MySQL架构篇【mysql高级】
2022.7.22DAY612
Now developers are beginning to do testing. Will there be no software testers in the future?
No valid host was found when setting up openstack to create an instance There are not enough hosts available. code:500
Matlab-二/三维图上绘制黑点
FTP service
Next item recommendations in short sessions
LeetCode剑指offer专项(一)整数
"Door lock" ignites a heated discussion on the safety of living alone. The new poster picture is suffocating
MySQL implementation plan
Unity Metaverse(二)、Mixamo & Animator 混合树与动画融合
How to close the high-level port
JWT快速入门
Copy pcap file with producer consumer model