当前位置:网站首页>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();
边栏推荐
- Dynamic performance view overview
- Shardingjdbc pit record
- 利用js实现统计字符串数组中各字符串出现的次数,并将其格式化为对象数组。
- utils 连接池
- 博途PLC一阶滞后系统传递函数阶跃响应输出仿真(SCL)
- Machine learning related competition website
- Shardingsphere data slicing
- Basic knowledge of convolutional neural network
- Fang Wenshan, Jay Chou's best partner, will officially announce "Hualiu yuancosmos" on July 25
- Selenium: detailed explanation of browser crawler use (I)
猜你喜欢

音视频学习(十)——ps流

程序环境和预处理

分布式相关面试题总结

Meta universe infrastructure: analysis of the advantages of Web 3.0 chain33

Jmeter性能测试之使用存储响应内容到文件监听器

Lambda and stream

ARIMA model for time series analysis and prediction
![[classic thesis of recommendation system (10)] Alibaba SDM model](/img/a5/3ae37b847042ffb34e436720f61d17.png)
[classic thesis of recommendation system (10)] Alibaba SDM model

PXE efficient batch network installation

Anaconda 中安装 百度飞浆Paddle 深度学习框架 教程
随机推荐
What is bloom filter in redis series?
JWT quick start
元宇宙基础设施:WEB 3.0 chain33 优势分析
Keras learning part: obtaining the output results of neural network middle layer
Using producer consumer model and dpkt to process pcap files
记一次路由器频繁掉线问题的分析、解决与发展
【uniapp】多种支付方式封装
The difference between overloading and rewriting
Practice of online question feedback module (XIV): realize online question answering function
Distributed system and distributed database system (Introduction)
Sort: merge sort and quick sort
[classic thesis of recommendation system (10)] Alibaba SDM model
总结软件测试岗的那些常见高频面试题
Hcip--- BGP comprehensive experiment
Regular expression rules and common regular expressions
Come across the sea to see you
OVSDB
1.MySQL架构篇【mysql高级】
Jmeter性能测试之将每次接口请求的结果保存到文件中
Kdd2022 | uncover the mystery of Kwai short video recommendation re ranking, and recommend the new SOTA