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