網(wǎng)站首頁 編程語言 正文
InputStream是java標準庫提供的最基本的輸入流,
而FileInputStream是InputStream的一個子類,F(xiàn)ileInputStream就是從文件流中讀取數(shù)據(jù)
例如:
public class Text {
? ? public static void main(String[] args) throws IOException {
? ? ? ? try (InputStream in = new FileInputStream("D:\\javadata\\BASE03\\Test1")) {
? ? ? ? ? ? int data = -1;
? ? ? ? ? ? while ((data = in.read()) != -1) {
? ? ? ? ? ? ? ? System.out.print(data);
? ? ? ? ? ? }
? ? ? ? } catch (IOException e) {
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
而BufferedInputStream是緩沖輸入流,使用緩沖區(qū)提高文件的讀取效率,繼承自FilterInputStream,
它的本質(zhì)是通過一個內(nèi)部緩沖區(qū)數(shù)組實現(xiàn)的
例如在新建某輸入流對應(yīng)的BufferedInputStream后,我們通過read()讀取輸入流的數(shù)據(jù)時,BufferedInputStream會將該輸入流的數(shù)據(jù)分批的填入到緩沖區(qū)中,每當緩沖區(qū)中的數(shù)據(jù)被讀完之后,輸入流會再次填充數(shù)據(jù)緩沖區(qū),如此反復(fù)直到我們讀完輸入流數(shù)據(jù)位置。
BufferedInputStream的作用是為其輸入流提供緩沖功能
創(chuàng)建BufferedInputStream時,需要通過它的構(gòu)造函數(shù)指定某個輸入流為參數(shù)
例如:
public class Main {
? ? public static void main(String[] args) throws IOException {
? ? ? ? // 指定要讀取文件的緩沖輸入字節(jié)流
? ? ? ? try (BufferedInputStream in = new BufferedInputStream(new ????????????????FileInputStream("D:\\javadata\\Text\\1.txt"))) {
? ? ? ? ? ? byte[] buff = new byte[128];// 用來存儲每次讀取到的字節(jié)數(shù)組
? ? ? ? ? ? int len;// 每次讀取到的字節(jié)數(shù)組的長度
? ? ? ? ? ? while ((len = in.read(buff)) != -1) {
? ? ? ? ? ? ? ? System.out.println(Arrays.toString(buff));
? ? ? ? ? ? }
? ? ? ? }catch (IOException e){
? ? ? ? ? ? e.printStackTrace();
? ? ? ? }
? ? }
}
原文鏈接:https://blog.csdn.net/qq_50587186/article/details/124972218
相關(guān)推薦
- 2023-10-11 MyBatis-plus wrapper.and用法 | apply
- 2022-08-24 C++中的Reactor原理與實現(xiàn)_C 語言
- 2022-11-28 基于GORM實現(xiàn)CreateOrUpdate方法詳解_Golang
- 2022-06-07 Sphinx生成python文檔示例圖文解析_python
- 2022-03-12 Nginx熱部署的實現(xiàn)_nginx
- 2022-07-12 Linux創(chuàng)建定時執(zhí)行任務(wù)
- 2023-03-27 Android進階之從IO到NIO的模型機制演進_Android
- 2022-08-13 瀏覽器的任務(wù)隊列-宏任務(wù)、微任務(wù)的執(zhí)行順序
- 最近更新
-
- window11 系統(tǒng)安裝 yarn
- 超詳細win安裝深度學習環(huán)境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎(chǔ)操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區(qū)別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權(quán)
- redisson分布式鎖中waittime的設(shè)
- maven:解決release錯誤:Artif
- restTemplate使用總結(jié)
- Spring Security之安全異常處理
- MybatisPlus優(yōu)雅實現(xiàn)加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務(wù)發(fā)現(xiàn)-Nac
- Spring Security之基于HttpR
- Redis 底層數(shù)據(jù)結(jié)構(gòu)-簡單動態(tài)字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應(yīng)用詳解
- 聊聊消息隊列,發(fā)送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支