当前位置:网站首页>FileInputStream与BufferedInputStream有哪些区别?
FileInputStream与BufferedInputStream有哪些区别?
2022-08-07 05:12:00 【一只码农菜汪】
前言
InputStream(字节输入流)是Java标准库提供的最基本的输入流。它在java.io这个包里,InputStream并不是一个接口而是一个抽象类,是所有抽象流的父类。其中最常用的两个子类为FileInputStream(文件输入流)和BufferedInputStream(缓冲输入流),那么FileInputStream与BufferedInputStream都有哪些区别呢?
一、FileInputStream(文件输入流)
FileInputStream就是从文件流中读取数据,创建一个FileInputStream的实例对象,相当于在磁盘和内存之间建立一个管道,对磁盘中的文件进行读(输入)操作。
注意:在创建对象的同时必须传入文件的路径
下面的代码分别演示了如何完整逐个字节读取和批量字节读取一个文件的所有字节。
1.逐个字节读取:
代码示例如下:
public static void main(String[] args) {
// 创建(实例化)"字节输入流"对象
//放在try()中会自动关闭,释放资源
try (InputStream in = new FileInputStream("D:\\tuji.txt")) {
// 保证每次读取到的字节值
int data = -1;
// 正常读取 :0-255
// 读取末尾 :-1
while ((data = in.read()) != -1) {
//逐个字节读取
System.out.print((char) data);//不能读中文 乱码
}
} catch (IOException e) {
e.printStackTrace();
}
}
2.批量字节读取:
代码示例如下:
public static void main(String[] args) {
try (InputStream in = new FileInputStream("D:\\tuji.txt")) {
//批量字节读取(缓冲)
byte[] buff = new byte[128];
//每次读取若干字节,填充到buff字节数组中
int len = -1;
while((len = in.read(buff)) != -1) {
System.out.printf("本次读取%d字节:%s\n",len,Arrays.toString(buff));
}
} catch (IOException e) {
e.printStackTrace();
}
二、BufferedInputStream(缓冲输入流)
BufferedInputStream是缓冲输入流,它继承于FilterInputStream。BufferedInputStream比FileInputStream的效率高,它的作用就是为另一个输入流添加一些和功能,例如,提供“缓冲功能”、支持“mark()标记”和“reset()重置方法”。BufferedInputStream其实就是在内存和磁盘之间通过字节数组(默认为8192)建立一个缓冲区,当要读取磁盘中的文件数据时,先通过FileInputStream从磁盘中读取数据,再分批将数据分批填入到缓冲区中。每当缓冲区的数据被读完,FileInputStream会在次填充数据缓冲区,直到读完文件数据。
注意:在创建对象时必须传入一个输入流对象
代码示例如下:
public static void main(String[] args) {
//带有缓冲区的字节输入流
//缓冲区大小为8192
try (BufferedInputStream bis = new BufferedInputStream(new FileInputStream("C:\\Users\\Lenovo\\Desktop\\javaEE\\Spring.png"))) {
//从内存的buff缓冲区中读取一个字节
int data = -1;
while((data = bis.read()) != -1) {
System.out.println(data);
}
} catch (IOException e) {
e.printStackTrace();
}
}
边栏推荐
- Redis关闭持久化
- 页面底部出现横向滚动条解决方法
- 洛谷P1218 特殊的质数肋骨 Superprime Rib
- 向量化引擎对HTAP的价值与技术思考
- happens-before规则与线程单例安全习题
- 线性代数学习笔记4-6:矩阵的零空间、列空间、行空间、左零空间、初等行变换、测验题
- 用C语言实现简单得通讯录
- Linear Algebra Study Notes 4-6: Null Space, Column Space, Row Space, Left Null Space, Elementary Row Transformation, Test Questions of Matrix
- 洛谷P1330 封锁阳光大学
- 云环境风险评估技术
猜你喜欢

【无标题】

谭浩强第五版第三章课后习题

docker 中安装 MySQL 以及使用

The setting and clearing of the inconsistency between the data displayed in the Excel cell and the actual data

【无标题】

线性代数学习笔记2-3补充:抽象向量空间

共享设备租赁小程序开发制作功能介绍
![[Graduation Project] Automatic gas station refueling system based on STM32 - Internet of Things, microcontroller, embedded](/img/29/ec3a7b2406d96bbf1de9b21c8a633d.png)
[Graduation Project] Automatic gas station refueling system based on STM32 - Internet of Things, microcontroller, embedded

Redis关闭持久化

全网最完整php禁用eval函数讲解
随机推荐
富滇银行完成数字化升级|OceanBase数据库助力布局分布式架构中台
"Postgraduate Research Ability Training and Cultivation"
共享设备租赁小程序开发制作功能介绍
一周活动速递|深入浅出第8期;Meetup成都站报名进行中
[Graduation Project] Automatic gas station refueling system based on STM32 - Internet of Things, microcontroller, embedded
ros1-gazebo创建世界和机器人模型(单线雷达、多线雷达、相机)
洛谷P1227 完美的对称
无聊的冷知识4
centos安装redis服务
Ansible - Explanation and Application of Playbook Playbook
IDEA:JSP发送post请求 控制台打印中文乱码
Excel合并单元格测试代码
Redis 常用数据类型之list(字符串列表)
人社部公布“数据库运行管理员”成新职业,OceanBase参与制定职业标准
预约家教老师上门辅导小程序开发制作功能介绍
[TypeScript Notes] 02 - TS Advanced Types
端到端的基于深度学习的网络入侵检测方法
phpoffice/phpspreadsheet的使用(export篇)
BPAAS化建设实践-基本流程篇
什么是负载均衡