字元流FileReader和FileWriter

2021-07-30 14:12:04 字數 2187 閱讀 1119

我們先來檢視一下filereader的api,

我們看到它是繼承inputstreamreader,所以我們直接使用inputstreamreader:先建立乙個檔案zzq.txt 然後我們使用inputstreamreader讀取裡面的內容,它會提示()裡面可以放乙個inputstream類,然後inputstream可以制定編碼格式,所以我們在()裡new乙個fileinputstream,再在後面加上「utf-8」編碼格式

/**

* 演示inputstreamreader的用法

*/public static void test1()

} catch (unsupportedencodingexception | filenotfoundexception e) catch (ioexception e)

}

接下來演示用陣列fileinpurstream讀取內容,我們先用file指定檔案,然後用

inpurstreamreader讀取

public static void test2() 

is.close();

} catch (unsupportedencodingexception | filenotfoundexception e) catch (ioexception e)

}

上面的方法,還要定義乙個陣列,可以使用io流的包裝類buffered,這樣,就可以直接讀取一行

/**

* 演示bufferedreader的用法,整行讀取文字

*/public static void test3()

} catch (unsupportedencodingexception | filenotfoundexception e) catch (ioexception e) finally catch (ioexception e)

} }

下面介紹filewrite的用法,檢視api可以知道它也是繼承outputstreamwrite

* 演示outputstreamwriter的用法

*/public static void test4() catch (filenotfoundexception e) catch (ioexception e)

}上面輸出的97因為不是string型別,所以會被電腦內部裝換成ascii碼,然後在zzq1.txt檔案中輸入小寫字母a

下面我們輸入中文試試,在fileoutputstream()裡面可以加乙個引數true,這樣我們之後寫入的內容就不會覆蓋之前寫的了

/**

* 演示outputstreamwriter的用法

*/public static void test5() catch (filenotfoundexception e) catch (ioexception e)

}

然後介紹乙個我認為的大王printwrite,怎麼說呢,總之如果需要使用輸出流就可以想到直接使用printwrite,下面看下api,可以看到printwrite()裡面可以直接放乙個檔案或者放乙個outputstream,這樣就可以使用輸出流的方法了

下面進行簡單的演示

/**

* 演示printwriter類的用法

*/public static void test6() catch (filenotfoundexception e)

}

以上就是字元流的簡單介紹

java IO流 字元流FileReader

1.字元流是什麼 字元流是可以直接讀寫字元的io流 字元流讀取字元,就要先讀取到位元組資料,然後轉為字元.如果要寫出字元,需要把字元轉為位元組再寫出.樣列 filereader fr new filereader txt int x fr.read system.out.println x char...

JAVA基礎知識之FileReader流

api文件說明 filereader流是用於讀取字元檔案的便捷類。此類的建構函式假定預設字元編碼和預設位元組緩衝區大小是合適的。如果要自己指定這些值,那麼需要通過fileinputstream流物件來構造inputstreamreader流物件即可 filereader類繼承inputstreamr...

Blob物件和FileReader介面

它表示二進位制原始資料,提供乙個slice方法,可以通過該方法訪問到位元組內部的原始資料快,file物件也繼承了這個blob物件 它有兩個屬性1.size屬性 表示乙個blob物件的長度 2.type屬性 表示blob的mime型別,如果是未知型別,則返回乙個空字串 blob物件使用示例 選擇檔案 ...