java基礎十 IO字元流Writer

2021-09-22 18:51:27 字數 3119 閱讀 1863

一、writer:用於寫入字元流的抽象類。 子類必須實現的唯一方法是write(char ,int,int),flush()和close()。 然而,大多數子類將覆蓋這裡定義的一些方法,以便提供更高的效率,附加的功能或兩者。

常用函式:

已知子類:  

1.bufferedwriter:將文字寫入字元輸出流,緩衝字元,以提供單個字元,陣列和字串的高效寫入。       

建構函式:

1.public bufferedwriter(writer out)       

常用函式:  

1.write(char cbuf, int off, int len) 寫入字元陣列的一部分。     

2.close() 關閉流,先重新整理。     

3.flush() 重新整理流。 

2.outputstreamwriter:可以實現位元組流轉字元流:它讀取位元組,並使用指定的charset將其解碼為字元。   

建構函式:

1.outputstreamwriter(outputstream out, charset cs)   

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。 

已知子類    

3. filewriter 是用於寫入字元流

建構函式:

1.filewriter(file file) 

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。 

4.chararraywriter :該類實現了可以用作writer的字元緩衝區。 當資料寫入流時,緩衝區會自動增長

建構函式:

1.chararraywriter(int initialsize) 

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。        

5.filterwriter:用於編寫過濾後的字元流的抽象類。 抽象類filterwriter本身提供了將所有請求傳遞給包含流的預設方法

建構函式:

1.chararraywriter(int initialsize) 

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。  

6.pipedwriter:管道流

建構函式:

1.pipedwriter(pipedreader snk) 

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。  

4.connect(pipedreader snk) 將此管道寫入器連線到接收器。 

7.stringwriter:字串流

建構函式:

1.stringwriter(int initialsize) 

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。  

4.getbuffer() 返回字串緩衝區本身。     

8.printwriter:列印流,將物件的格式表示列印到文字輸出流

建構函式:

1.printwriter(outputstream out) 

2.printwriter(file file) 

3.printwriter(writer out) 

常用函式:

1.close() 關閉流,先重新整理。 

2.flush() 重新整理流。

3.write(char cbuf, int off, int len) 寫入字元陣列的一部分。  

4.print(string s) 列印字串。  

常用的例項**

//filewriter 檔案最簡單的用法

public static void filewritertest(string data,string path)

//給乙個file物件構造乙個filewriter物件。 如果第二個引數是true ,則位元組將寫入檔案的末尾而不是開頭。

filewriter=new filewriter(pathtest,true);

filewriter.write(data);

//重新整理流,將資料寫入文字

filewriter.flush();

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

}//filewriter增加緩衝技術的用法,使用bufferedwriter包裝類

public static void filewritertest2(string data, string path) throws ioexception

bufferedwriter bufferedwriter=null;

try finally }}

//位元組流轉字元流

@requiresapi(api = build.version_codes.kitkat)

public static void outputstreamwritertest(string data, outputstream out) catch (ioexception e)

}

java基礎筆記之IO流之字元流

知識點 字元流 是直接讀取字元的io流 字元輸入流 reader 讀的時候,是把位元組轉成字元,然後再讀取 filereader 字元輸入流 bufferedreader 特有方法 public string readline 一次讀取一行,讀不到返回null,讀到就返回對應的資料 string型別...

java的IO流 字元流

reader 是所有的輸入字元流的父類,它是乙個抽象類。reader 中各個類的用途和使用方法基本和inputstream 中的類使用一致。後面會有reader 與inputstream 的對應關係。1 看看幾個常用的實現類 該類從資料來源讀取位元組並將其解碼為使用指定的字符集的字元。inputst...

java基礎之io流

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