📜  Java中的Reader read()方法和示例

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

Java中的Reader read()方法和示例

Java中Reader类的read()方法用于从流中读取单个字符。此方法阻塞流直到:

  • 它从流中获取了一些输入。
  • 发生了一些 IOException
  • 它在读取时已到达流的末尾。

该方法被声明为抽象方法。这意味着如果在读取字符时需要更改所需的操作,Reader 抽象类的子类应该重写此方法。

句法:

public abstract int read()

参数:此方法不接受任何参数

返回值:此方法返回一个整数值,它是从流中读取的整数值。它的范围可以从 0 到 65535。如果没有读取任何字符,则返回 -1。

异常:如果输入输出时发生错误,此方法将抛出IOException

下面的方法说明了 read() 方法的工作:

方案一:

// Java program to demonstrate
// Reader read() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
  
            String str = "GeeksForGeeks";
  
            // Create a Reader instance
            Reader reader
                = new StringReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first 5 characters
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            for (int i = 0; i < 5; i++) {
                ch = reader.read();
                System.out.println("\nInteger value "
                                   + "of character read: "
                                   + ch);
                System.out.println("Actual "
                                   + "character read: "
                                   + (char)ch);
            }
  
            reader.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
Integer value of character read: 71
Actual character read: G

Integer value of character read: 101
Actual character read: e

Integer value of character read: 101
Actual character read: e

Integer value of character read: 107
Actual character read: k

Integer value of character read: 115
Actual character read: s

方案二:

// Java program to demonstrate
// Reader read() method
  
import java.io.*;
import java.util.*;
  
class GFG {
    public static void main(String[] args)
    {
  
        try {
            String str = "GeeksForGeeks";
  
            // Create a Reader instance
            Reader reader
                = new StringReader(str);
  
            // Get the character
            // to be read from the stream
            int ch;
  
            // Read the first character
            // to this reader using read() method
            // This will put the str in the stream
            // till it is read by the reader
            ch = reader.read();
            System.out.println("\nInteger value "
                               + "of character read: "
                               + ch);
            System.out.println("Actual "
                               + "character read: "
                               + (char)ch);
  
            reader.close();
        }
        catch (Exception e) {
            System.out.println(e);
        }
    }
}
输出:
Integer value of character read: 71
Actual character read: G

参考: https://docs.oracle.com/javase/9/docs/api/ Java/io/Reader.html#read–