Java InputStreamReader getEncoding()方法及示例

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
### 回答1: Java InputStreamReaderJava IO 流包中的一个类,它可以将字节流转换为字符流。它通过使用给定的字符集或平台的默认字符集将字节流解码为字符。InputStreamReader 是一个桥接器,它将字节流转换为字符流。 ### 回答2: Java中的InputStreamReader是一个用于将字节流转化为字符流的桥梁。它将字节输入流转化为字符输入流,可以根据指定的字符编码将字节解码成字符。 InputStreamReader是Reader的子类,它通过使用指定的字符集将字节流解码成字符流。它使用的字符集可以是系统默认的字符集,也可以是通过指定的字符编码来实现。 使用InputStreamReader,我们可以从字节输入流中读取字节并将其解码为相应的字符,然后用于处理文本数据。例如,可以将InputStreamReader与BufferedReader组合使用,一次读取一行字符,并对其进行处理。 当我们需要读取文本文件或从网络连接中接收文本数据时,InputStreamReader非常有用。它可以从文件、网络连接、InputStream等字节流中读取内容,并将其解码为字符,方便处理和操作。 以下是使用InputStreamReader的一个简单示例: ```java import java.io.*; public class InputStreamReaderExample { public static void main(String[] args) { try { InputStream inputStream = new FileInputStream("input.txt"); InputStreamReader reader = new InputStreamReader(inputStream, "UTF-8"); int data = reader.read(); while (data != -1) { char character = (char) data; System.out.print(character); data = reader.read(); } reader.close(); } catch (IOException e) { e.printStackTrace(); } } } ``` 在上面的示例中,我们从文件“input.txt”中读取字符流并打印出来。使用InputStreamReader,我们可以指定所需的字符编码,这里我们使用UTF-8编码进行解码。 总之,InputStreamReaderJava中用于将字节输入流转化为字符输入流的工具,可以将字节解码为字符。 ### 回答3: InputStreamReaderJava.io 包中的一个类,用于将字节流转换为字符流。 它是字节流和字符流之间的桥梁。它继承自 Reader 类,是一个输入流读取器。 InputStreamReader 的构造方法接收一个字节输入流(例如 FileInputStream) 和一个指定的字符编码方式(例如 UTF-8)。 它将字节输入流中的字节数据根据指定的字符编码方式转换成相应的字符。 InputStreamReader 提供了 read() 方法用于从输入流中读取字符,返回一个整型的字符数值。 read() 方法会一次读取一个字符,当读取到流末尾时返回 -1。 InputStreamReader 还提供了一个重载的 read() 方法,它接收一个字符数组的参数,并读取指定长度的字符到数组中。 此外,InputStreamReader 还提供了 close() 方法用于关闭输入流。 使用 InputStreamReader 的好处是它可以处理不同字符编码方式的输入流,并将其转换成统一的字符流。 这在处理不同编码方式的文本数据时非常有用,可以确保读取到的字符数据是正确的。 需要注意的是,在使用 InputStreamReader 时要确保输入流的编码方式与指定的字符编码方式一致, 否则可能会导致读取到的字符数据出现乱码或错误。此外,还要注意及时关闭输入流,以释放资源并避免内存泄露。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值