简体   繁体   English

java InputStreamReader / BufferedReader的“读取”行为

[英]java InputStreamReader / BufferedReader “read” behavior

A related question is this one: Where is the specification that defines this behavior for InputStreamReader? 一个相关的问题是: 为InputStreamReader定义此行为的规范在哪里? , but I'm not sure if it answers mine... Please note, I'm just experimenting with the language. ,但不确定是否能回答我的问题。请注意,我只是在尝试使用该语言。 I have this code: 我有以下代码:

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Capitalize {

    public static void main(String[] args) {

        try (BufferedReader br = new BufferedReader(new InputStreamReader(
                System.in))) {

            char c;
            do {
                c = Character.toUpperCase((char) br.read());
                System.out.print(c);
            } while (c != (char) -1);
        } catch (IOException e) {
            e.printStackTrace(System.err);
        }
    }

}

Using ubuntu linux, I was expecting the output to be like this: 使用ubuntu linux,我期望输出如下所示:

fFoOoO  bBaArR

but instead, it's like this: 但相反,它是这样的:

foo bar (line feed)
FOO BAR (waits for more characters or Ctrl + D)

Right now I'm not sure about what is the behavior in windows, probably it's different, but still, this confuses me a bit. 现在,我不确定Windows中的行为是什么,可能有所不同,但这仍然让我感到困惑。 Reading the documentation for the read method, I see it will only return -1 if the end of stream is reached. 阅读有关read方法的文档,我看到如果到达流的末尾,它将仅返回-1。 I kind of understand how that would work for reading a file, but how about, in this case, the console? 我有点理解读取文件的方式,但是在这种情况下,控制台又如何呢? Why does it have to wait until the Ctrl + D is typed? 为什么必须等到Ctrl + D键入? Is there any way to get to the end of the stream without having to type Ctrl + D? 有什么方法可以不必键入Ctrl + D来到达流的末尾吗? Is there a way to achieve what I was expecting? 有没有办法实现我的期望?

Thanks in advance 提前致谢

  1. As EJP comments, this is nothing to do with InputStream / BufferedReader. 正如EJP所说,这与InputStream / BufferedReader无关。 What you are seeing is the behaviour of the Linux "tty" input drivers and a typical console command. 您将看到Linux“ tty”输入驱动程序的行为以及一个典型的控制台命令。

  2. What you are seeing is normal "line editing" or "line buffering". 您所看到的是正常的“行编辑”或“行缓冲”。 The input is only made available for Java to read (via the stdin file descriptor) when you press the <Enter> key. 当按下<Enter>键时,输入仅可用于Java读取(通过stdin文件描述符)。 The output you are seeing prior to that is the character echoing from the "tty" drivers. 之前看到的输出是从“ tty”驱动程序回显的字符。 (And notice that if you enter <Backspace> or whatever ... the characters get erased. When you type <Enter> , Java doesn't see the backspaces, etc. They have been edited out.) (并且请注意,如果输入<Backspace>或任何...,字符将被删除。当您键入<Enter> ,Java将看不到退格键等。它们已被编辑。)

  3. Input via the java.io.Console class behaves the same way. 通过java.io.Console类进行的输入行为相同。

  4. This is the same on Windows: the behaviour is portable. 在Windows上是相同的:行为是可移植的。 (This is a good thing ...) (这是一件好事...)

  5. If you want your Java application to see the characters as they are typed, when they are typed, you will need to use 3rd-party libraries (or native code) to implement this. 如果希望Java应用程序在键入字符时看到它们,则在键入字符时,需要使用3rd-party库(或本机代码)来实现。 (In Linux, it entails switching the tty driver into "raw" mode ... and this functionality is non-portable, and not supported by standard Java SE.) (在Linux中,这需要将tty驱动程序切换为“原始”模式...并且此功能是不可移植的,并且标准Java SE不支持。)

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

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