java io詳解三 位元組輸入輸出流

2021-09-01 09:14:53 字數 3705 閱讀 7500

這篇部落格我們講的是位元組輸入輸出流:inputstream、outputsteam(下圖紅色長方形框內),紅色橢圓框內是其典型實現(fileinputsteam、fileoutstream)

1、位元組輸出流:outputstream

publicabstractclassoutputstream

extendsobject

implementscloseable, flushable

這個抽象類是表示位元組輸出流的所有類的超類。 輸出流接收輸出位元組並將其傳送到某個接收器。

方法摘要:

下面我們用 位元組輸出流 outputstream 的典型實現 fileoutputstream 來介紹:

//1、建立目標物件,輸出流表示把資料儲存到哪個檔案。不寫碟符,預設該檔案是在該項目的根目錄下

file target = new file("io"+file.separator+"a.txt");

//2、建立檔案的位元組輸出流物件,第二個引數是 boolean 型別,true 表示後面寫入的檔案追加到資料後面,false 表示覆蓋

outputstream out = new fileoutputstream(target,true);

//3、具體的 io 操作(將資料寫入到檔案 a.txt 中)

/*** void write(int b):把乙個位元組寫入到檔案中

* void write(byte b):把陣列b 中的所有位元組寫入到檔案中

* void write(byte b,int off,int len):把陣列b 中的從 off 索引開始的 len 個位元組寫入到檔案中

*/out.write(65); //將 a 寫入到檔案中

out.write("aa".getbytes()); //將 aa 寫入到檔案中

out.write("abcdefg".getbytes(), 1, 5); //將 bcdef 寫入到檔案中

//經過上面的操作,a.txt 檔案中資料為 aaabcdef

//4、關閉流資源

out.close();

system.out.println(target.getabsolutepath());

2、位元組輸入流:inputstreampublicabstractclassinputstreamextendsobjectimplementscloseable這個抽象類是表示輸入位元組流的所有類的超類。

方法摘要:

下面我們用 位元組輸出流 inputstream 的典型實現 fileinputstream 來介紹:

//1、建立目標物件,輸入流表示那個檔案的資料儲存到程式中。不寫碟符,預設該檔案是在該項目的根目錄下

儲存的檔案內容為:aaabcdef

file target = new file("io"+file.separator+"a.txt");

//2、建立輸入流物件

inputstream in = new fileinputstream(target);

//3、具體的 io 操作(讀取 a.txt 檔案中的資料到程式中)

/*** 注意:讀取檔案中的資料,讀到最後沒有資料時,返回-1

* int read():讀取乙個位元組,返回讀取的位元組

* int read(byte b):讀取多個位元組,並儲存到陣列 b 中,從陣列 b 的索引為 0 的位置開始儲存,返回讀取了幾個位元組

* int read(byte b,int off,int len):讀取多個位元組,並儲存到陣列 b 中,從陣列b 的索引為 0 的位置開始,長度為len個位元組

*///int read():讀取乙個位元組,返回讀取的位元組

int data1 = in.read();//獲取 a.txt 檔案中的資料的第乙個位元組

system.out.println((char)data1); //a

//int read(byte b):讀取多個位元組儲存到陣列b 中

byte buffer = new byte[10];

in.read(buffer);//獲取 a.txt 檔案中的前10 個位元組,並儲存到 buffer 陣列中

system.out.println(arrays.tostring(buffer)); //[65, 97, 66, 67, 68, 69, 70, 0, 0, 0]

system.out.println(new string(buffer)); //aabcdef

//int read(byte b,int off,int len):讀取多個位元組,並儲存到陣列 b 中,從索引 off 開始到 len

in.read(buffer, 0, 3);

system.out.println(arrays.tostring(buffer)); //[65, 97, 66, 0, 0, 0, 0, 0, 0, 0]

system.out.println(new string(buffer)); //aab

//4、關閉流資源

in.close();

3、用位元組流完成檔案的複製

/**

* 將 a.txt 檔案 複製到 b.txt 中

*///1、建立源和目標

file srcfile = new file("io"+file.separator+"a.txt");

file descfile = new file("io"+file.separator+"b.txt");

//2、建立輸入輸出流物件

inputstream in = new fileinputstream(srcfile);

outputstream out = new fileoutputstream(descfile);

//3、讀取和寫入操作

byte buffer = new byte[10];//建立乙個容量為 10 的位元組陣列,儲存已經讀取的資料

int len = -1;//表示已經讀取了多少個位元組,如果是 -1,表示已經讀取到檔案的末尾

while((len=in.read(buffer))!=-1)

//4、關閉流資源

out.close();

in.close();

IO框架(三)位元組緩衝流

目錄bufferedoutputstream bufferedinputstream方法 protected byte buf 儲存資料的內部緩衝區陣列。protected int count 索引一大於緩衝區中最後乙個有效位元組的索引。protected int marklimit mark方法呼...

CRC CRC推導(三)位元組查表與半位元組查表

上節我們講到模二除法,模二除法的操作是按位進行,每次移動一位,然後計算,演算法要套2個迴圈,效率較低。由於異或運算有交換律和結合律。因此,我們再次看一下範例 傳送資料為0xcf16 1100111100010110b 多項式為0x11021 10001000000100001b 為例,結果如下 我們...

51微控制器彙編三位元組加法

乙個加數在片內ram40h 41h 42h單元中,另乙個加數在內ram43h 44h 45h,其和存放在50h 51h 52h單元中,進製位存00h。求編譯程式,小弟謝過 最佳答案 題目太簡單,只有三個位元組參加運算,可不用迴圈結構。mov a,40h add a,43h mov 50h,a mov...