IO的使用說明

2021-07-04 05:51:30 字數 3144 閱讀 1341

io有字元流和位元組流兩種,位元組流可以讀取任何資料(、二進位制、純文字、properties...)字元流只能讀純文字。切記!!!!!

怎麼看要用相對應的io類 一、

源:inputstream,reader

目的:outputstream,writer 二、

明確具體的裝置。

是硬碟就加file;

是鍵盤用system.in(是乙個inputstream物件);

是記憶體用陣列;是網路用socket流。

同樣目的是哪個裝置:

是硬碟就加file;

是鍵盤用system.out(是乙個outoutstream物件);

是記憶體用陣列;是網路用socket流。 三、

明確是否還需要其他額外功能呢,例如

①是否需要較高的效率,即是否需要使用緩衝區,是就加上buffered;

②是否需要轉換,是,就使用轉換流,inputstreamreader 和outputstreamwriter。

一般不用他,一般用fileinputstream

構造可以用file也可以直接寫路徑

讀取方法:

while((i = input.read())!=-1)

當read()返回-1的時候說明到尾了!

但是還有兩個read()用法

int

read()

從此輸入流中讀取乙個資料位元組。

int

read(byte b)

從此輸入流中將最多b.length個位元組的資料讀入乙個 byte 陣列中。

int

read(byte b, int off, int len)

從此輸入流中將最多len個位元組的資料讀入乙個 byte 陣列中。

如第二個用法:

例1:byte buf = new byte[12];

int len = 0;

while((len =input.read(buf))!=-1)

例2:

byte

buf = 

newbyte[12

];  

intlen = in.read(buf);  

system.out.println(buf);  

system.out.println(new

string(buf, 

0, len));  

一般不用reader,因為如果使用就要重寫其中的兩個方法,所以一般使用filereader或者bufferreader

filereader:

讀取方法:

while((i=reader.read())!=-1)這裡和inputstream的一樣,但是注意他沒有readline()方法!!

bufferreader:

他有readlin()方法!不過他的構造需要乙個filereader物件!!

讀取方法:

bufferedreader breader = new bufferedreader(reader);

string line = null;

while((line=breader.readline())!=null)

一行行讀取!!

寫檔案:

fileoutputstream:

file file = null;

fileoutputstream output = null;

try catch (exception e) finally catch (ioexception e)

}在構造的時候回發現有多重構造方法:

fileoutputstream(file file)

建立乙個向指定file物件表示的檔案中寫入資料的檔案輸出流。

fileoutputstream

建立乙個向指定file物件表示的檔案中寫入資料的檔案輸出流。

fileoutputstream(filedescriptor fdobj)

建立乙個向指定檔案描述符處寫入資料的輸出檔案流,該檔案描述符表示乙個到檔案系統中的某個實際檔案的現有連線。

fileoutputstream(string name)

建立乙個向具有指定名稱的檔案中寫入資料的輸出檔案流。

fileoutputstream

建立乙個向具有指定name的檔案中寫入資料的輸出檔案流。

fileoutputstream

filewriter  :

filewriter  wt = null;

try catch (ioexception e) finally catch (ioexception e) }

回車是\t\n

byte和string相互轉換

1、string 轉 byte

string str = "hello";

byte srtbyte = str.getbytes();

2、byte 轉 string

byte srtbyte;

string res = new string(srtbyte);

system.out.println(res);

3、設定編碼方式相互轉換

string str = "hello";

byte srtbyte = null;

try  catch (unsupportedencodingexception e) 

其中有涉及帶編碼:

utf-8、gbk

使用說明 附註工具使用說明

附註工具使用說明 附註工具用途 附註工具主要用於更新利用word附註應用程式生成的帶域 的附註,該工具在word右鍵 更新鏈結 的基礎上進行了優化,故在使用時,不能再利用word右鍵 更新鏈結 而要用本工具的 更新當前鏈結 或 更新所有鏈結 使用說明 一 更換路徑 當利用word附註應用程式生成帶域...

this的使用說明

student類中 private string name public void setname string name main函式中 student stu1 newstudent stu1.setname 小冪冪 此時main函式中stu1儲存了student類的記憶體位址,當stu1呼叫s...

使用說明 農用遮光網使用說明

農用遮光網使用說明 建築防塵網購買方 建築施工企業 在購買建築防塵網時,應該對 作出比較,可以分辨品牌 型號,且購買時應該在一定程度上了解信譽良莠。建築防塵網購買方 市場售賣方 在選購建築防塵網時,可以把 作為基礎,好的 可以用來彌補信譽不足,而差的 則需要按照你的服務收費。農用遮光網使用說明 用途...