Java IO流之原理分類與節(jié)點(diǎn)流文件操作詳解
IO流簡(jiǎn)介
I/O是Input/Output的縮寫, I/O技術(shù)是非常實(shí)用的技術(shù),用于處理設(shè)備之間的數(shù)據(jù)傳輸。如讀/寫文件,網(wǎng)絡(luò)通訊等。
Java程序中,對(duì)于數(shù)據(jù)的輸入/輸出操作以“流(stream)” 的方式進(jìn)行。
java.io包下提供了各種“流”類和接口,用以獲取不同種類的數(shù)據(jù),并通過(guò)標(biāo)準(zhǔn)的方法輸入或輸出數(shù)據(jù)。
IO流原理
輸入input:讀取外部數(shù)據(jù)(磁盤、光盤等存儲(chǔ)設(shè)備的數(shù)據(jù))到程序(內(nèi)存)中。
輸出output:將程序(內(nèi)存)數(shù)據(jù)輸出到磁盤、光盤等存儲(chǔ)設(shè)備中。
流的分類
①按操作數(shù)據(jù)單位不同分為:字節(jié)流(8 bit 一般用于非文本文件),字符流(16 bit 一般用于文本文件)
②按數(shù)據(jù)流的流向不同分為:輸入流,輸出流(相對(duì)的)
③按流的角色的不同分為:節(jié)點(diǎn)流(直接處理文件),處理流(處理被包含的流)
IO 流體系
Java的IO流共涉及40多個(gè)類,實(shí)際上非常規(guī)則,都是從如下4個(gè)抽象基類派生的。
由這四個(gè)類派生出來(lái)的子類名稱都是以其父類名作為子類名后綴。
節(jié)點(diǎn)流和處理流
節(jié)點(diǎn)流:直接從數(shù)據(jù)源或目的地讀寫數(shù)據(jù)
處理流:不直接連接到數(shù)據(jù)源或目的地,而是“連接”在已存在的流(節(jié)點(diǎn)流或處理流)之上,通過(guò)對(duì)數(shù)據(jù)的處理為程序提供更為強(qiáng)大的讀寫功能。
節(jié)點(diǎn)流操作
讀入以FileReader為例
import java.io.File; import java.io.FileReader; import java.io.IOException; /** * @Author: Yeman * @Date: 2021-09-25-16:30 * @Description: */ public class FileReaderTest { public static void main(String[] args) { FileReader fileReader = null; try { //一定需要try-catch //1、實(shí)例化File對(duì)象,指明要操作的文件 File file = new File("IO\\hello.txt"); //2、提供具體的流 fileReader = new FileReader(file); //3、讀取操作 int read = fileReader.read(); //空參為一位一位讀取,末尾返回-1 while (read != -1){ System.out.print((char) read); read = fileReader.read(); } } catch (IOException e) { e.printStackTrace(); } finally { //4、關(guān)閉流 try { if (fileReader != null) { //確保不會(huì)因具體流未創(chuàng)建而產(chǎn)生空指針異常 fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
import java.io.File; import java.io.FileReader; import java.io.IOException; /** * @Author: Yeman * @Date: 2021-09-25-16:30 * @Description: */ public class FileReaderTest { public static void main(String[] args) { FileReader fileReader = null; try { //一定需要try-catch //1、實(shí)例化File對(duì)象,指明要操作的文件 File file = new File("IO\\hello.txt"); //2、提供具體的流 fileReader = new FileReader(file); //3、讀取操作 char[] chars = new char[5]; //char型數(shù)組為參數(shù),該數(shù)組相當(dāng)于一個(gè)容器,把讀取放在里面,返回該次讀取的個(gè)數(shù),末尾返回-1 // 最后若不夠,容器后部分仍為上一次取的,前部分則被新的這次取到的覆蓋了 int length = fileReader.read(chars); while (length != -1){ for (int i = 0; i < length; i++) { System.out.print(chars[i]); } length = fileReader.read(chars); } } catch (IOException e) { e.printStackTrace(); } finally { //4、關(guān)閉流 try { if (fileReader != null) { fileReader.close(); } } catch (IOException e) { e.printStackTrace(); } } } }
寫出以FileWriter為例
import java.io.*; /** * @Author: Yeman * @Date: 2021-09-25-16:30 * @Description: */ public class FileReaderTest { public static void main(String[] args) { FileWriter fw1 = null; //若硬盤中不存在file,創(chuàng)建之;若存在,內(nèi)容覆蓋之 try { //1、實(shí)例化File對(duì)象,指明要寫出的文件 File file = new File("IO\\hi.txt"); //2、創(chuàng)建具體的流 fw1 = new FileWriter(file); //FileWriter fw2 = new FileWriter(file,false); //若硬盤中不存在file,創(chuàng)建之;若存在,內(nèi)容覆蓋之 //FileWriter fw3 = new FileWriter(file,true); //若硬盤中不存在file,創(chuàng)建之;若存在,內(nèi)容追加之 //3、寫出操作 fw1.write("Hello World!\n",0,5); //寫出“Hello” fw1.write("你好,世界!"); //寫出“你好,世界!” } catch (IOException e) { e.printStackTrace(); } finally { //4、關(guān)閉流 try { if (fw1 != null) fw1.close(); } catch (IOException e) { e.printStackTrace(); } } } }
實(shí)現(xiàn)一個(gè)圖片復(fù)制(讀入寫出,使用字節(jié)流)
import java.io.*; /** * @Author: Yeman * @Date: 2021-09-25-16:30 * @Description: */ public class FileReaderTest { public static void main(String[] args) { FileInputStream fis = null; FileOutputStream fos = null; try { File inFile = new File("IO\\input.jpg"); File outFile = new File("IO\\output.jpg"); fis = new FileInputStream(inFile); fos = new FileOutputStream(outFile); byte[] bytes = new byte[1024]; //通常使用1024,2的10次方 int length = fis.read(bytes); while (length != -1){ fos.write(bytes,0,length); length = fis.read(bytes); } } catch (IOException e) { e.printStackTrace(); } finally { try { if (fos != null) fos.close(); } catch (IOException e) { e.printStackTrace(); } try { if (fis != null) fis.close(); } catch (IOException e) { e.printStackTrace(); } } } }
到此這篇關(guān)于Java IO流之原理分類與節(jié)點(diǎn)流文件操作詳解的文章就介紹到這了,更多相關(guān)Java IO流內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
SpringCloud Config使用本地倉(cāng)庫(kù)及map注入
這篇文章主要介紹了SpringCloud Config使用本地倉(cāng)庫(kù)及map注入,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-09-09Java JDK動(dòng)態(tài)代理的基本原理詳細(xì)介紹
這篇文章主要介紹了Java JDK動(dòng)態(tài)代理的基本原理詳細(xì)介紹的相關(guān)資料,這里對(duì)動(dòng)態(tài)代理進(jìn)行了詳解并附簡(jiǎn)單實(shí)例代碼,需要的朋友可以參考下2017-01-01SpringSecurity+JWT實(shí)現(xiàn)前后端分離的使用詳解
這篇文章主要介紹了SpringSecurity+JWT實(shí)現(xiàn)前后端分離的使用詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2021-01-01Spring?Boot中的過(guò)濾器攔截器監(jiān)聽(tīng)器使用技巧匯總
本文將介紹在Spring?Boot應(yīng)用程序中使用過(guò)濾器、攔截器和監(jiān)聽(tīng)器的使用技巧,我們將討論它們之間的區(qū)別,以及何時(shí)使用它們,我們還將提供代碼示例,以幫助您在自己的應(yīng)用程序中使用它們2023-12-12java通過(guò)復(fù)選框控件數(shù)組實(shí)現(xiàn)添加多個(gè)復(fù)選框控件示例分享
編寫程序,通過(guò)復(fù)選框控件數(shù)組事先選擇用戶愛(ài)好信息的復(fù)選框,在該程序中,要求界面中的復(fù)選框數(shù)量可以根據(jù)指定復(fù)選框名稱的字符串?dāng)?shù)組的長(zhǎng)度來(lái)自動(dòng)調(diào)節(jié)2014-02-02