鴻蒙OS InputStreamReader

2022-10-20 14:31 更新

InputStreamReader

java.lang.Object

|---java.io.Reader

|---|---java.io.InputStreamReader

public class InputStreamReader
extends Reader

InputStreamReader 是從字節(jié)流到字符流的橋梁:它讀取字節(jié)并使用指定的字符集將它們解碼為字符。 它使用的字符集可以由名稱(chēng)指定,也可以顯式給出,或者可以接受平臺(tái)的默認(rèn)字符集。

InputStreamReader 的 read() 方法之一的每次調(diào)用都可能導(dǎo)致從底層字節(jié)輸入流中讀取一個(gè)或多個(gè)字節(jié)。 為了實(shí)現(xiàn)字節(jié)到字符的有效轉(zhuǎn)換,可以從底層流中預(yù)先讀取比滿(mǎn)足當(dāng)前讀取操作所需的更多的字節(jié)。

為了獲得最高效率,請(qǐng)考慮將 InputStreamReader 包裝在 BufferedReader 中。 例如:

 BufferedReader in
   = new BufferedReader(new InputStreamReader(System.in));

Since:

JDK1.1

字段摘要

從類(lèi) java.io.Reader 繼承的字段
lock

構(gòu)造函數(shù)摘要

構(gòu)造函數(shù) 描述
InputStreamReader(InputStream in) 創(chuàng)建一個(gè)使用默認(rèn)字符集的 InputStreamReader。
InputStreamReader(InputStream in, String charsetName) 創(chuàng)建一個(gè)使用命名字符集的 InputStreamReader。
InputStreamReader(InputStream in, Charset cs) 創(chuàng)建一個(gè)使用給定字符集的 InputStreamReader。
InputStreamReader(InputStream in, CharsetDecoder dec) 創(chuàng)建一個(gè)使用給定字符集解碼器的 InputStreamReader。

方法總結(jié)

修飾符和類(lèi)型 方法 描述
void close() 關(guān)閉流并釋放與其關(guān)聯(lián)的任何系統(tǒng)資源。
String getEncoding() 返回此流使用的字符編碼的名稱(chēng)。
int read() 讀取單個(gè)字符。
int read(char[] cbuf, int offset, int length) 將字符讀入數(shù)組的一部分。
boolean ready() 告訴這個(gè)流是否準(zhǔn)備好被讀取。
從類(lèi) java.lang.Object 繼承的方法
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
從類(lèi) java.io.Reader 繼承的方法
mark, markSupported, read, read, reset, skip

構(gòu)造函數(shù)詳細(xì)信息

InputStreamReader

public InputStreamReader(InputStream in)

創(chuàng)建一個(gè)使用默認(rèn)字符集的 InputStreamReader。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
in 輸入流

InputStreamReader

public InputStreamReader(InputStream in, String charsetName) throws UnsupportedEncodingException

創(chuàng)建一個(gè)使用命名字符集的 InputStreamReader。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
in 輸入流
charsetName 支持的字符集的名稱(chēng)

Throws:

Throw名稱(chēng) Throw描述
UnsupportedEncodingException 如果不支持命名字符集

InputStreamReader

public InputStreamReader(InputStream in, Charset cs)

創(chuàng)建一個(gè)使用給定字符集的 InputStreamReader。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
in 輸入流
cs 一個(gè)字符集

Since:

1.4

InputStreamReader

public InputStreamReader(InputStream in, CharsetDecoder dec)

創(chuàng)建一個(gè)使用給定字符集解碼器的 InputStreamReader。

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
in 輸入流
dec 一個(gè)字符集解碼器

Since:

1.4

方法詳情

getEncoding

public String getEncoding()

返回此流使用的字符編碼的名稱(chēng)。

如果編碼具有歷史名稱(chēng),則返回該名稱(chēng); 否則返回編碼的規(guī)范名稱(chēng)。

如果此實(shí)例是使用 InputStreamReader(java.io.InputStream,java.lang.String) 構(gòu)造函數(shù)創(chuàng)建的,則返回的名稱(chēng)(對(duì)于編碼而言是唯一的)可能與傳遞給構(gòu)造函數(shù)的名稱(chēng)不同。 如果流已關(guān)閉,此方法將返回 null。

返回:

此編碼的歷史名稱(chēng),如果流已關(guān)閉,則為 null

read

public int read() throws IOException

讀取單個(gè)字符。

覆蓋:

read in class Reader

返回:

讀取的字符,如果已到達(dá)流的末尾,則為 -1

Throws:

Throw名稱(chēng) Throw描述
IOException 如果發(fā)生 I/O 錯(cuò)誤

read

public int read(char[] cbuf, int offset, int length) throws IOException

將字符讀入數(shù)組的一部分。

指定者:

read in class Reader

參數(shù):

參數(shù)名稱(chēng) 參數(shù)描述
cbuf 目標(biāo)緩沖區(qū)
offset 開(kāi)始存儲(chǔ)字符的偏移量
length 要讀取的最大字符數(shù)

返回:

讀取的字符數(shù),如果已到達(dá)流的末尾,則為 -1

Throws:

Throw名稱(chēng) Throw描述
IOException 如果發(fā)生 I/O 錯(cuò)誤

ready

public boolean ready() throws IOException

告訴這個(gè)流是否準(zhǔn)備好被讀取。 如果 InputStreamReader 的輸入緩沖區(qū)不為空,或者可以從底層字節(jié)流中讀取字節(jié),則 InputStreamReader 已準(zhǔn)備就緒。

覆蓋:

ready in class Reader

返回:

如果保證下一個(gè) read() 不會(huì)阻塞輸入,則為 true,否則為 false。 請(qǐng)注意,返回 false 并不能保證下一次讀取將阻塞。

Throws:

Throw名稱(chēng) Throw描述
IOException 如果發(fā)生 I/O 錯(cuò)誤

close

public void close() throws IOException

從類(lèi)復(fù)制的描述:Reader

關(guān)閉流并釋放與其關(guān)聯(lián)的任何系統(tǒng)資源。 關(guān)閉流后,進(jìn)一步的 read()、ready()、mark()、reset() 或 skip() 調(diào)用將引發(fā) IOException。 關(guān)閉以前關(guān)閉的流沒(méi)有效果。

指定者:

在接口 AutoCloseable 中關(guān)閉

指定者:

在接口Closeable中關(guān)閉

指定者:

close in class Reader

Throws:

Throw名稱(chēng) Throw描述
IOException 如果發(fā)生 I/O 錯(cuò)誤
以上內(nèi)容是否對(duì)您有幫助:
在線(xiàn)筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號(hào)
微信公眾號(hào)

編程獅公眾號(hào)