当前位置: 首页>>代码示例 >>用法及示例精选 >>正文


Java InputStreamReader用法及代码示例


InputStreamReader class

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


相关用法


注:本文由纯净天空筛选整理自佚名大神的英文原创作品 InputStreamReader class in Java。非经特殊声明,原始代码版权归原作者所有,本译文未经允许或授权,请勿转载或复制。