📜  Java中的 InputStreamReader 类

📅  最后修改于: 2022-05-13 01:55:35.348000             🧑  作者: Mango

Java中的 InputStreamReader 类

InputStreamReader 类

InputStreamReader 是从字节流到字符流的桥梁。它读取字节并使用指定的字符集将它们解码为字符。它使用的字符集可以由名称指定,也可以显式给出,或者可以接受平台的默认字符集。
宣言 :

public class InputStreamReader
   extends Reader

构造函数:

  • InputStreamReader(InputStream in_strm) :创建一个使用默认字符集的 InputStreamReader。
  • InputStreamReader(InputStream in_strm, Charset cs) :创建一个使用给定字符集的 InputStreamReader。
  • InputStreamReader(InputStream in_strm, CharsetDecoder dec) :创建一个使用给定字符集解码器的 InputStreamReader。
  • InputStreamReader(InputStream in_strm, String charsetName) :创建一个使用命名字符集的 InputStreamReader

方法:

  • ready() : Java.io.InputStreamReader.ready()告诉字符流是否准备好被读取。如果 InputStreamReader 的输入缓冲区不为空,或者可以从底层字节流中读取字节,则 InputStreamReader 已准备就绪。
    句法 :
public boolean ready()
Returns :
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 并释放与其关联的所有 Streams。关闭流后,进一步的 read()、ready()、mark()、reset() 或 skip() 调用将引发 IOException。
    句法 :
public void close()
Returns :
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");
        }
    }
}


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");
        }
    }
}


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”文件,本文中的所有程序都不会在在线 IDE 上运行。您可以在系统上的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 : 
Returns :
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 :
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
  • 本文由Mohit Gupta 贡献。如果您喜欢 GeeksforGeeks 并愿意做出贡献,您还可以使用 write.geeksforgeeks.org 撰写文章或将您的文章邮寄至 review-team@geeksforgeeks.org。在 GeeksforGeeks 主页上查看您的文章并帮助其他 Geeks。
    如果您发现任何不正确的地方,或者您想分享有关上述主题的更多信息,请写下评论。