java 基礎之 I O流 其三 (文字輸入輸出)

2021-08-05 20:24:59 字數 3964 閱讀 9042

上一小節講了位元組的使用,但是使用位元組的操作不適合我們在日常中對檔案的文字進行使用和理解,所以這節講講字元流的操作。

儲存資料時,我們可以選擇文字格式printwrite 或 二進位制格式dataoutputstream。

一 . 文字格式儲存:在儲存文字字串時,需要考慮字元編碼。理由如下:

編碼格式

內容編碼

utf-16

「1234」

00 31 00 32 00 33 00 34

ios 8859-1

「1234」

31 32 33 34

(當然我們日常中會造成亂碼的原因是 編碼 和解碼使用的編碼集不同)

inputstreamreader 類將包含位元組的輸入流 換成可以產生 unicode 碼元的讀入器

inputstreamreader in  = new inputstreamreader(new ffileinputstream("changetom.dat"),"iso8859-1");
outputstreamreader 類將選定字元編碼方式,將unicode字元流轉成位元組流

1.輸出文字 - - printwriter 類

printwriter out = new printwriter("changetom.dat");

//等同於

printwriter out = new printwriter(new filewrite("changetom.dat"),true);

//後面的true是啟用沖刷機制,只要println被呼叫,就會被送到目的地;(預設情況是關閉,要等到關閉流才會送到目的地)

string name = "nihao";

out.print("asd");

out.print(name);

//最終會轉換成位元組並寫入changetom.dat 檔案中

2.輸入文字 - - bufferedreader 和 scanner

(bufferedreader 1.5 之前 而1.6之後 可以使用 scanner )

bufferedreader:要和 輸入源組合起來

(補充:這裡bufferedreader 具有緩衝的功能 。之所以要使用緩衝,是因為在操作讀寫的時候,程式一邊讀一邊寫是非常傷硬碟,速度也不快,而建立緩衝區可以先讀取固定長度的內容,放到記憶體,再從記憶體寫到硬碟。)

//從檔案輸入

try(bufferedreader bufin = new bufferedreader(

new inputstreamreader(new fileinputstream("emplyes.dat"),"utf-8")))

} catch (filenotfoundexception e) catch (ioexception e)

//從控制台輸入

bufferedreader bufin = new bufferedreader(new inputstreamreader(system.in))

scanner的使用方式

scanner in = new scanner(system.in);

string str = in

.next();

// 輸入整型

int a = in

.nextint();

system.out

.println(str);

system.out

.println(a);

scanner 和 bufferedreader 兩哥們的區別

bufferreader 只能讀取字元型別,識別基本型別

scanner 是用正規表示式處理過了的,所以可以識別基本型別

3.輸入文字和輸出文字組合使用

//控制台輸入

try(bufferedreader bufin = new bufferedreader(

new inputstreamreader(system.in));

printwriter out = new printwriter(

new fileoutputstream("emplyes.dat"))) catch (filenotfoundexception e) catch (ioexception e)

二、二進位制資料讀寫繼承於位元組流 dataoutputstream 和 datainputstream

使用的方式和其他流無益,這裡只貼出**

try(bufferedreader bufin = new bufferedreader(

new inputstreamreader(system.in));

//使用二進位製流輸出到檔案

dataoutputstream out = new dataoutputstream(

new fileoutputstream("emplyes.dat"))) catch (filenotfoundexception e) catch (ioexception e)

三、隨機訪問檔案 randomaccessfile :實現了dataoutput 和 datainput 介面

特性:

randomaccessfile的第二個構造引數進行讀寫控制

「r」 :唯讀 。如果試圖對該randomaccessfile指定的檔案執行寫入方法則會丟擲ioexception

「rw」 :讀寫 。如果檔案不存在,會嘗試建立檔案

「rwd」 :讀寫 。有rw功能,還要求對檔案的內容的每個更新都要同步到儲存裝置上。這對確保在系統崩潰時不會丟失重要資訊特別有用。

「rws」:讀寫。有rwd 的功能,還要求對元資料的每個更新都要同步到儲存裝置上。

randomaccessfile 主要方法

讀取

//randomaccessfile 讀取

try(randomaccessfile raf = new randomaccessfile("emplyes.dat","r"))

} catch (filenotfoundexception e) catch (ioexception e)

寫入

這裡很簡單,只需要移動指標,再寫入資料就行

//寫出檔案

try(randomaccessfile raf = new randomaccessfile("emplyes.dat","rw")) catch (filenotfoundexception e) catch (ioexception e)

讀寫,

在檔案中間插入資料的操作

try(randomaccessfile raf = new randomaccessfile("emplyes.dat","rw"))

//指標再返回原來的地方

raf.seek(4);

raf.write("這是在讀寫操作中插入的資料".getbytes());

//將臨時檔案的內容插回去

while((hasread = fis.read(buff))>0)

}} catch (filenotfoundexception e) catch (ioexception e)

Java基礎 Java重點基礎之IO流(三)

一,序列流 2.使用方式 fileinputstream fis1 new fileinputstream a.txt 建立輸入流物件,關聯a.txt fileinputstream fis2 new fileinputstream b.txt 建立輸入流物件,關聯b.txt sequenceinp...

java基礎之io流

1.四大抽象基類 位元組流 inputstream outputstream 字元流 writer reader 位元組輸出流寫檔案用其子類fileoutputstream類 構造 fileoutputstream file file 傳遞file物件包裝檔案 string name 傳遞字串型別檔...

java基礎之IO流

io流概念 輸入流 把能夠讀取乙個位元組序列的物件稱為輸入流。輸出流 把能夠寫乙個位元組序列的物件稱為輸出流。通俗理解 對於初學者,可能常常不清楚何時該用輸入流,何時該用輸出流。本人將這兩個流記為 讀入寫出 那麼我就清楚輸入流就有read 讀 方法,輸出流就有write 寫 方法。然後,再思考 讀入...