InputStreamReader 是一座來自字節流到字符流。它讀取字節並使用指定的字符集將它們解碼為字符。它使用的字符集可以通過名稱指定或可以顯式給出,或者可以接受平台的默認字符集。
聲明:
public class InputStreamReader extends Reader
構造函數:
- 輸入流讀取器(輸入流in_strm):創建使用默認字符集的InputStreamReader。
- InputStreamReader(InputStream in_strm,字符集cs):創建使用給定字符集的InputStreamReader。
- InputStreamReader(InputStream in_strm, CharsetDecoder dec):創建使用給定字符集解碼器的InputStreamReader。
- InputStreamReader(InputStream in_strm, 字符串字符集名稱):創建使用命名字符集的 InputStreamReader
方法:
- ready(): java.io.InputStreamReader.ready()指示字符流是否已準備好讀取。如果 InputStreamReader 的輸入緩衝區不為空,或者可以從底層字節流中讀取字節,則 InputStreamReader 已準備就緒。
句法:
public boolean ready() 返回: True : if the Character stream is ready to be read False : if the Character stream is not ready to be read
- close(): java.io.InputStreamReader.close()關閉InputStreamReader並釋放與其關聯的所有流。關閉流後,進一步的 read()、ready()、mark()、reset() 或 skip() 調用將拋出 IOException。
句法:
public void close() 返回: No value is returned
- ready()和close()方法的實現:
Java
// Java program illustrating ready() and close() method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
try
{
// initializing FileInputStream
FileInputStream geek = new FileInputStream("ABC.txt");
// Initializing InputStreamReader object
InputStreamReader in_strm = new InputStreamReader(geek);
int t;
while((t=in_strm.read())!= -1)
{
// convert the integer true to character
char r = (char)t;
System.out.println("Character : "+r);
// check if the stream in_strm ready
boolean b = in_strm.ready();
// Use of ready() methods
System.out.println("Ready? : "+b);
}
// Use of close() method to Close InputStreamReader
in_strm.close();
// Closing FileInputStream
geek.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println("NO Such File Exists");
}
catch (IOException except)
{
System.out.println("IOException occurred");
}
}
}
- 筆記:
本文中的所有程序都不會在在線 IDE 上運行,因為不存在“ABC”文件。您可以在係統上的 Java 編譯器上檢查此代碼。
要檢查此代碼,請在您的係統上創建一個文件“ABC”。
“ABC”文件包含:
極客
為了
極客
輸出:
Character : G Ready? : true Character : e Ready? : true Character : e Ready? : true Character : k Ready? : true Character : s Ready? : true Character : Ready? : true Character : Ready? : true Character : Ready? : true Character : F Ready? : true Character : o Ready? : true Character : r Ready? : true Character : Ready? : true Character : Ready? : true Character : Ready? : true Character : G Ready? : true Character : e Ready? : true Character : e Ready? : true Character : k Ready? : true Character : s Ready? : false
- getEncoding(): java.io.InputStreamReader.getEncoding()返回該流所使用的字符編碼的名稱。
句法:
public String getEncoding() Parameters : 返回: No value is returned
- getEncoding()方法的實現:
Java
// Java program illustrating getEncoding() method
import java.io.*;
public class NewClass
{
public static void main(String[] args)
{
try
{
// initializing FileInputStream
FileInputStream geek = new FileInputStream("ABC.txt");
// Initializing InputStreamReader object
InputStreamReader in_strm = new InputStreamReader(geek);
// Use of getEncoding() method
// to get the character encoding present in the stream
String encoding = in_strm.getEncoding();
System.out.println("Encoding used : "+encoding);
// Closing InputStreamReader
in_strm.close();
// Closing FileInputStream
geek.close();
}
catch (FileNotFoundException fnfe)
{
System.out.println("NO Such File Exists");
}
catch (IOException except)
{
System.out.println("IOException occurred");
}
}
}
- 輸出:
Encoding used : UTF8
- read(): java.io.InputStreamReader.read()讀取後返回單個字符。
句法:
public int read() 返回: Returns single character after reading or -1 if the end of the stream has been reached
- 執行:
Java
// Java program illustrating read() method
import java.io.*;
public class NewClass
{
public static void main(String[] args) throws FileNotFoundException, IOException
{
// initializing FileInputStream
FileInputStream geek = new FileInputStream("ABC.txt");
// Initializing InputStreamReader object
InputStreamReader in_strm = new InputStreamReader(geek);
int t;
String read_reslt="";
// Use of read() method
while((t = in_strm.read()) != -1)
{
read_reslt = read_reslt+(char)t;
}
// print the result read from the file
System.out.println(read_reslt);
}
}
- 筆記:
“ABC”文件包含:
1
極客
2
為了
3
極客
輸出:
1 Geeks 2 For 3 Geeks
相關用法
- Java InputStreamReader close()用法及代碼示例
- Java InputStreamReader getEncoding()用法及代碼示例
- Java InputStreamReader ready()用法及代碼示例
- Java InputStream available()用法及代碼示例
- Java InputStream close()用法及代碼示例
- Java InputStream mark()用法及代碼示例
- Java InputStream markSupported()用法及代碼示例
- Java InputStream reset()用法及代碼示例
- Java InputStream skip()用法及代碼示例
- Java InputStream轉String用法及代碼示例
- Java InputStream和OutputStream的區別用法及代碼示例
- Java Integer divideUnsigned()用法及代碼示例
- Java Integer equals()用法及代碼示例
- Java Integer getInteger()用法及代碼示例
- Java Integer longValue()用法及代碼示例
- Java Integer max()用法及代碼示例
- Java Integer min()用法及代碼示例
- Java Integer numberOfLeadingZeros()用法及代碼示例
- Java Integer numberOfTrailingZeros()用法及代碼示例
- Java Integer parseInt()用法及代碼示例
- Java Integer parseUnsignedInt()用法及代碼示例
- Java Integer remainderUnsigned()用法及代碼示例
- Java Integer reverseBytes(int i)用法及代碼示例
- Java Integer toBinaryString()用法及代碼示例
- Java Integer toHexString()用法及代碼示例
注:本文由純淨天空篩選整理自佚名大神的英文原創作品 InputStreamReader class in Java。非經特殊聲明,原始代碼版權歸原作者所有,本譯文未經允許或授權,請勿轉載或複製。