简体   繁体   English

与Java中的缓冲读取器混淆

[英]confused with buffered reader in java

  1. when i run the code only with //1 - start, the output is ascii value of input character 当我仅使用// 1-start运行代码时,输​​出为输入字符的ascii值
  2. when i run the code only with //2 - start, the output is the entered string terminated by newline character 当我仅使用// 2-开始运行代码时,输​​出为输入的字符串,以换行符终止
  3. when i run the code with both (as shown in below code), only //1 - start executes, readLine() is being considered in a strange way. 当我同时运行两个代码时(如下面的代码所示),只有// 1-开始执行,以一种奇怪的方式考虑了readLine()。
  4. when i run the code with both (with //2 - start placed above //1 - start), both the codes executes fine, 当我同时运行两个代码时(// 2-开始放在// 1-开始上方),两个代码都可以正常执行,

Please explain why this strange behavior happens in case 3 but not in case 4. 请解释为什么这种奇怪的情况在情况3中发生而在情况4中没有发生。

public class InputBufferedReader {

  public static void main(String[] args) {
    BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
    try {
        //1-start
        //read one character
        System.out.println("enter input: ");
        int b= br.read();
        System.out.println(b);
        //end

        //2-start
        //read a string
        System.out.println("enter input string: ");
        String a = br.readLine();
        System.out.println(a);
        //end
    } catch (IOException e) {
        e.printStackTrace();
    }
  }
}

Your BufferedReader uses the InputStreamReader with System.in . 您的BufferedReaderInputStreamReaderSystem.in The BufferedReader uses the read() method from InputStreamReader to read the data from the standard input stream System.in . BufferedReader使用InputStreamReaderread()方法从标准输入流System.in读取数据。 Now lets look into the API for this read() method. 现在,让我们看一下该read()方法的API。

[...] This method blocks until input data is available, the end of the stream is detected, or an exception is thrown. [...]该方法将阻塞,直到输入数据可用,检测到流的末尾或引发异常为止。

Blocking means in this context waits for user to input data throu the console confirming with the Enter Key. 在这种情况下,阻塞意味着等待用户通过控制台输入数据并使用Enter键确认。
With that in mind lets examine your cases. 考虑到这一点,让我们检查一下您的案例。
1. int b= br.read(); 1. int b= br.read(); Nothing is already typed, so this method blocks until user typed something and then prints the ascci value of first character. 由于尚未输入任何内容,因此该方法将阻塞,直到用户键入某些内容,然后打印第一个字符的ascci值。
2. String a = br.readLine(); 2. String a = br.readLine(); Nothing is already typed, so this method blocks until user typed something and then prints the whole line. 由于尚未输入任何内容,因此该方法将阻塞,直到用户键入某些内容,然后再打印整行。
3. 3。

int b= br.read();

Lets image user typed a confirming with Enter Key that means the input is a\\n . 让图像用户键入a与输入键则意味着输入是确认a\\n Now read() reads the first character which is a . 现在read()读取第一个字符是a

String a = br.readLine();

This read() call will not block and ask for user input because there is unconsumed input left \\n . read()调用不会阻止并要求用户输入,因为\\n剩下未使用的输入。 So readLine() will read \\n . 因此readLine()将读取\\n
4. 4。

String a = br.readLine();

User is asked for input which is confirmed with Enter Key. 要求用户输入并用Enter键确认。 And the whole line will be read. 整行将被读取。

int b= br.read();

There are no unconsumed data left, because readLine() already has read the whole line including \\n character. 没有剩余的未使用数据,因为readLine()已经读取了包括\\n字符在内的整行。 So this read() call blocks and user is asked for input. 因此,此read()调用被阻止,并要求用户输入。

int read() method attempts to Read Next Character from the Console(or File) and Return its Unicode Value , int read()方法尝试从控制台(或文件)读取下一个字符并返回其Unicode值

As this Method Returns Unicode Value Compulsory at the Time of Printing we- should perform type- Casting. 由于此方法在打印时返回Unicode必填值,因此我们应执行类型转换。

If there is No Next Character the we will get -1 . 如果没有下一个字符,我们将得到-1

String readLine() Method attempts to Read Next Line from Console(or File) and Returns it, if it is available. 字符串readLine()方法尝试从控制台(或文件)读取下一行并返回(如果有)。

If the Next line is Not available, then return null. 如果“下一行”不可用,则返回null。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM