对 Java 中 System.in 的 read() 方法的行为感到困惑
confusion about the behavior of the read() method of System.in in Java
我知道Systemclass的System.in是InputStream的具体subclass的一个实例,因为InputStream的read()方法是抽象的System.in 必须覆盖此方法。
根据关于 InputStream 的 read() 方法的文档:
public abstract int read() throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.
Returns:
the next byte of data, or -1 if the end of the stream is reached.
Throws:
IOException - if an I/O error occurs.
如果到达流的末尾,read() 方法应该 return -1。我的问题是,什么时候 System.in.read() return -1?
示例代码如下:
import java.io.*;
class SystemInTest{
public static void main(String[] args) throws IOException{
InputStream in = System.in;
//InputStream in = new FileInputStream("h.txt");
int ch = 0;
while((ch = in.read()) != -1){
System.out.println(ch);
}
}
}
运行 这段代码,然后输入 "abc" 后跟一个 "Enter",结果是(在 Linux 下):
97
98
99
10
然后应用程序被阻塞并等待另一个输入。但我认为 while 循环 "ch = in.read()" 中的语句在读取行终止字符并在控制台上打印 10 后应该继续 运行 和 return -1。如果是这样,则应终止该应用程序。但是它被阻止了。
作为比较,如果我取消注释注释行,使用内容为 "abc\n" 的文件作为字节输入流,那么应用程序会按预期终止,因为 -1 是 returned。
System.in.read() 从来没有 returns -1 是真的吗?如果是这样,为什么 System.in 中的 read() 方法的实现与 InputStream 的其他子 class 不同,例如 FileInputStream?
输入流没有固定大小,因此您的程序进入无限循环并一次又一次地请求输入。
但是,h.txt 文件具有固定大小,因此 while 正在终止。
按Enter
只表示完成了一行,并不表示完成了整个“文件”。
完成文件的方式取决于操作系统。在 Linux 上,它是 Ctrl+D
,您可以在许多程序中使用它来退出它们(而不是键入 exit
或 quit
)。在 Windows 上,它是 Ctrl+Z
。
我知道Systemclass的System.in是InputStream的具体subclass的一个实例,因为InputStream的read()方法是抽象的System.in 必须覆盖此方法。 根据关于 InputStream 的 read() 方法的文档:
public abstract int read() throws IOException
Reads the next byte of data from the input stream. The value byte is returned as an int in the range 0 to 255. If no byte is available because the end of the stream has been reached, the value -1 is returned. This method blocks until input data is available, the end of the stream is detected, or an exception is thrown.
A subclass must provide an implementation of this method.Returns:
the next byte of data, or -1 if the end of the stream is reached.Throws:
IOException - if an I/O error occurs.
如果到达流的末尾,read() 方法应该 return -1。我的问题是,什么时候 System.in.read() return -1?
示例代码如下:
import java.io.*;
class SystemInTest{
public static void main(String[] args) throws IOException{
InputStream in = System.in;
//InputStream in = new FileInputStream("h.txt");
int ch = 0;
while((ch = in.read()) != -1){
System.out.println(ch);
}
}
}
运行 这段代码,然后输入 "abc" 后跟一个 "Enter",结果是(在 Linux 下):
97
98
99
10
然后应用程序被阻塞并等待另一个输入。但我认为 while 循环 "ch = in.read()" 中的语句在读取行终止字符并在控制台上打印 10 后应该继续 运行 和 return -1。如果是这样,则应终止该应用程序。但是它被阻止了。
作为比较,如果我取消注释注释行,使用内容为 "abc\n" 的文件作为字节输入流,那么应用程序会按预期终止,因为 -1 是 returned。
System.in.read() 从来没有 returns -1 是真的吗?如果是这样,为什么 System.in 中的 read() 方法的实现与 InputStream 的其他子 class 不同,例如 FileInputStream?
输入流没有固定大小,因此您的程序进入无限循环并一次又一次地请求输入。 但是,h.txt 文件具有固定大小,因此 while 正在终止。
按Enter
只表示完成了一行,并不表示完成了整个“文件”。
完成文件的方式取决于操作系统。在 Linux 上,它是 Ctrl+D
,您可以在许多程序中使用它来退出它们(而不是键入 exit
或 quit
)。在 Windows 上,它是 Ctrl+Z
。