JAVA中的位元組流和字元流的轉換

2021-07-22 20:59:00 字數 922 閱讀 1395

以字元為導向的stream基本上有與之相對應的以位元組為導向的stream,兩個對應類實現的功能相同,只是操作時的導向不同

1:位元組輸入流轉換為字元輸入流:

inputstreamreader是位元組流向字元流的橋梁,它使用指定的charset讀取位元組並將其解碼為字元,它使用的字符集可以由名稱指定或顯示給定。根據inputstream的例項建立inputstreamreader的方法有4種:

inputstreamreader(inputstream in)//根據預設字符集建立

inputstreamreader(inputstream in,charset cs)//使用給定字符集建立

inputstreamreader(inputstream in,charsetdecoder dec)//使用給定字符集解碼器建立

inputstreamreader(inputstream in,string charsetname)//使用指定字符集建立

2:位元組輸出流轉換為字元輸出流

outputstreamwriter是字元流通向位元組流的橋梁,它使用指定的charset將要寫入流中的字元編碼成位元組,它使用的字符集可以由名稱指定或顯示給定,否則將接受預設的字符集:

根據根據inputstream的例項建立outputstreamwriter的方法有4種:

outputstreamwriter(outputstream out)//根據預設的字符集建立

outputstreamwriter(outputstream out,charset cs)//使用給定的字符集建立

outputstreamwriter(outputstream out,charsetdecoder dec)//使用組定字符集建立

outputstreamwriter(outputstream out,string charsetname)//使用指定字符集建立

Java位元組流和字元流的區別

簡單來說 位元組流不使用快取,字元流使用快取。位元組流直接把資料寫到檔案,而字元流先把資料寫到快取,再寫到檔案。寫檔案時 使用位元組流的時候,wirte之後,就算沒有關閉流 close方法 程式也把資料寫到檔案了 使用字元流的時候,write之後,如果沒有關閉流,開啟檔案後發現資料並沒有寫進去。當時...

java中的位元組流和字元流的儲存比較

用幾個常用型別的資料來比較位元組流和字元流的區別 int a 5 boolean b true char c g string d 你好 使用字元流列印以上型別的資料到檔案中 printwriter dos new printwriter new bufferedwriter new filewri...

java中的位元組流和字元流的儲存比較

引用 用幾個常用型別的資料來比較位元組流和字元流的區別 int a 5 boolean b true char c g string d 你好 使用字元流列印以上型別的資料到檔案中 printwriter dos new printwriter new bufferedwriter new file...