網站首頁 編程語言 正文
InputStream是java標準庫提供的最基本的輸入流,
而FileInputStream是InputStream的一個子類,FileInputStream就是從文件流中讀取數據
例如:
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是緩沖輸入流,使用緩沖區提高文件的讀取效率,繼承自FilterInputStream,
它的本質是通過一個內部緩沖區數組實現的
例如在新建某輸入流對應的BufferedInputStream后,我們通過read()讀取輸入流的數據時,BufferedInputStream會將該輸入流的數據分批的填入到緩沖區中,每當緩沖區中的數據被讀完之后,輸入流會再次填充數據緩沖區,如此反復直到我們讀完輸入流數據位置。
BufferedInputStream的作用是為其輸入流提供緩沖功能
創建BufferedInputStream時,需要通過它的構造函數指定某個輸入流為參數
例如:
public class Main {
? ? public static void main(String[] args) throws IOException {
? ? ? ? // 指定要讀取文件的緩沖輸入字節流
? ? ? ? try (BufferedInputStream in = new BufferedInputStream(new ????????????????FileInputStream("D:\\javadata\\Text\\1.txt"))) {
? ? ? ? ? ? byte[] buff = new byte[128];// 用來存儲每次讀取到的字節數組
? ? ? ? ? ? int len;// 每次讀取到的字節數組的長度
? ? ? ? ? ? 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
相關推薦
- 2022-08-01 Python?OpenCV基于HSV的顏色分割實現示例_python
- 2022-06-04 C#復雜XML反序列化為實體對象兩種方式小結_C#教程
- 2022-10-14 Redis緩存擊穿解決方案之互斥鎖
- 2022-06-10 C#關鍵字Check簡單介紹_C#教程
- 2023-02-07 python正則表達式實現自動化編程_python
- 2022-09-08 pytorch?tensor內所有元素相乘實例_python
- 2022-09-08 Pytorch中transforms.Resize()的簡單使用_python
- 2023-03-17 Python中threading.Timer()定時器實現定時任務_python
- 最近更新
-
- window11 系統安裝 yarn
- 超詳細win安裝深度學習環境2025年最新版(
- Linux 中運行的top命令 怎么退出?
- MySQL 中decimal 的用法? 存儲小
- get 、set 、toString 方法的使
- @Resource和 @Autowired注解
- Java基礎操作-- 運算符,流程控制 Flo
- 1. Int 和Integer 的區別,Jav
- spring @retryable不生效的一種
- Spring Security之認證信息的處理
- Spring Security之認證過濾器
- Spring Security概述快速入門
- Spring Security之配置體系
- 【SpringBoot】SpringCache
- Spring Security之基于方法配置權
- redisson分布式鎖中waittime的設
- maven:解決release錯誤:Artif
- restTemplate使用總結
- Spring Security之安全異常處理
- MybatisPlus優雅實現加密?
- Spring ioc容器與Bean的生命周期。
- 【探索SpringCloud】服務發現-Nac
- Spring Security之基于HttpR
- Redis 底層數據結構-簡單動態字符串(SD
- arthas操作spring被代理目標對象命令
- Spring中的單例模式應用詳解
- 聊聊消息隊列,發送消息的4種方式
- bootspring第三方資源配置管理
- GIT同步修改后的遠程分支