当前位置:网站首页>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
边栏推荐
- Introduction and use of redis
- Pride-pppar source code analysis
- Basic DOS commands
- Several high-frequency JVM interview questions
- MySQL Database Constraints
- TYUT太原理工大学2022软工导论考试题型大纲
- MYSQL索引钟B-TREE ,B+TREE ,HASH索引之间的区别和应用场景
- 2年经验总结,告诉你如何做好项目管理
- MySQL backup -- common errors in xtrabackup backup
- Shortest Hamilton path (pressure DP)
猜你喜欢
Chromatic judgement bipartite graph
Heap sort [handwritten small root heap]
Common method signatures and meanings of Iterable, collection and list
TYUT太原理工大学2022“mao gai”必背
Interview Essentials: talk about the various implementations of distributed locks!
Tyut Taiyuan University of technology 2022 introduction to software engineering summary
Answer to "software testing" exercise: Chapter 1
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
MySQL 30000 word essence summary + 100 interview questions, hanging the interviewer is more than enough (Collection Series
面渣逆袭:Redis连环五十二问,三万字+八十图详解。
随机推荐
On March 15, the official version of go 1.18 was released to learn about the latest features and usage
Shortest Hamilton path (pressure DP)
Role movement in the first person perspective
[GNSS data processing] Helmert variance component estimation analysis and code implementation
TYUT太原理工大学2022数据库大题之E-R图转关系模式
12 excel charts and arrays
All in one 1405: sum and product of prime numbers
如何保障 MySQL 和 Redis 的数据一致性?
[GNSS] robust estimation (robust estimation) principle and program implementation
《软件测试》习题答案:第一章
Implementation of Excel import and export functions
Share a website to improve your Aesthetics
Summary of multiple choice questions in the 2022 database of tyut Taiyuan University of Technology
几道高频的JVM面试题
Decomposition relation model of the 2022 database of tyut Taiyuan University of Technology
Employment of cashier [differential constraint]
[algorithm] sword finger offer2 golang interview question 6: sum of two numbers in the sorting array
Error: sorting and subscript out of bounds
阿里云一面:并发场景下的底层细节 - 伪共享问题
Database operation of tyut Taiyuan University of technology 2022 database