InputStreamReader類的getEncoding()方法 (InputStreamReader Class getEncoding() method)
getEncoding() method is available in java.io package.
getEncoding()方法在java.io包中可用。
getEncoding() method is used to get the encoding name avail for this InputStreamReader stream and it returns a historical encoding name when it exists otherwise it returns canonical encoding name.
getEncoding()方法用于獲取此InputStreamReader流的編碼名稱,如果存在,則返回歷史編碼名稱,否則返回規范編碼名稱。
getEncoding() method is a non-static method, it is accessible with the class object only and if we try to access the method with the class name then we will get an error.
getEncoding()方法是一個非靜態方法,只能通過類對象訪問,如果嘗試使用類名稱訪問該方法,則會收到錯誤消息。
getEncoding() method does not throw an exception at the time of getting encoding.
getEncoding()方法在獲取編碼時不會引發異常。
Syntax:
句法:
public String getEncoding();
Parameter(s):
參數:
It does not accept any parameter.
它不接受任何參數。
Return value:
返回值:
The return type of the method is String, it gets historical character encoding name when exists otherwise it returns canonical encoding name or it may return null when this stream has been closed.
該方法的返回類型為String ,如果存在則獲取歷史字符編碼名稱,否則返回規范編碼名稱,或者在關閉此流時返回null。
Example:
例:
// Java program to demonstrate the example
// of String getEncoding() method
// of InputStreamReader
import java.io.*;
public class Demo1 {
public static void main(String[] args) throws Exception {
InputStream is_stm = null;
InputStreamReader isr_stm = null;
int val = 0;
try {
// Instantiates FileInputStream and InputStreamReader
is_stm = new FileInputStream("D:\\includehelp.txt");
isr_stm = new InputStreamReader(is_stm);
// By using getEncoding() method is to
// get the character encoding used by the
// stream isr_stm
String encoding = isr_stm.getEncoding();
System.out.println("isr_stm.getEncoding(): " + encoding);
} catch (Exception ex) {
System.out.println(ex.toString());
} finally {
// with the help of this block is to
// free all necessary resources linked
// with the stream
if (is_stm != null) {
is_stm.close();
if (isr_stm != null) {
isr_stm.close();
}
}
}
}
}
Output
輸出量
isr_stm.getEncoding(): Cp1252
翻譯自: https://www.includehelp.com/java/inputstreamreader-getencoding-method-with-example.aspx