为什么新的InputStreamReader不会读取控制台中的其余字符? [英] Why a new InputStreamReader won't read the remaining characters in the console?
问题描述
所以我有一个用Java编写的非常简单的服务器:
So I have a very simple server written in Java:
public class SimpleServer {
public static void main(String[] args) throws Exception {
ServerSocket serverSocket = new ServerSocket(8888);
System.out.println("Server Socket created, waiting for client...");
Socket accept = serverSocket.accept();
InputStreamReader inputStreamReader = new InputStreamReader(accept.getInputStream());
int read;
System.out.println("Client connected, waiting for input");
while ((read = inputStreamReader.read()) != -1) {
System.out.print((char) read);
}
}
}
这是一个代码我用来连接它:
And here is a code that I use to connect to it:
public class SimpleClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost",8888);
OutputStreamWriter outputStream = new OutputStreamWriter(socket.getOutputStream());
InputStreamReader inputStreamReader;
char[] chars = new char[5];
while (true) {
System.out.println("Say something: ");
inputStreamReader = new InputStreamReader(System.in);
inputStreamReader.read(chars);
int x = 0;
for (int i=0;i<5;i++) {
if(chars[i]!='\u0000') {
x++;
}
}
outputStream.write(chars,0,x);
outputStream.flush();
chars = new char[5];
}
}
}
现在,当我在客户终端输入类似的内容时:
Now when I type something like this in the terminal of the Client:
123456789
我将在服务器的终端中看到:
I will see in the terminal of the Server:
Server Socket created, waiting for client...
Client connected, waiting for input
12345
但是,当我按如下方式更改客户时:
However, when I change client as follows:
public class SimpleClient {
public static void main(String[] args) throws Exception {
Socket socket = new Socket("localhost",8888);
OutputStreamWriter outputStream = new OutputStreamWriter(socket.getOutputStream());
InputStreamReader inputStreamReader = new InputStreamReader(System.in);
char[] chars = new char[5];
while (true) {
System.out.println("Say something: ");
inputStreamReader.read(chars);
int x = 0;
for (int i=0;i<5;i++) {
if(chars[i]!='\u0000') {
x++;
}
}
outputStream.write(chars,0,x);
outputStream.flush();
chars = new char[5];
}
}
}
然后对于相同的输入,我将看到:
then for the same input, I will see:
Server Socket created, waiting for client...
Client connected, waiting for input
123456789
我的问题是, System.out 是一个静态变量,在这种情况下已经打开并连接到终端。为什么创建新的InputStreamReader对象时终端中的信息会丢失?相同的终端传递给对象,不是吗?
My question is, System.out is a static variable which is already open and connected to the terminal in this case. Why is the information in the terminal lost when a new InputStreamReader object is created? Same terminal is passed to the object, isn't it?
推荐答案
为什么在创建新的InputStreamReader对象时终端中的信息会丢失?
Why is the information in the terminal lost when a new InputStreamReader object is created?
当您在 InputStreamReader <上调用
read()
时/ code>,它允许(并且经常会)从流中读取比实际请求更多的数据,并将其余数据存储在缓冲区中,以满足以后的读取
来电。我怀疑整个文本行实际上是由第一个 InputStreamReader
读取的,所以当你构造第二个 时对于同一个流,InputStreamReader
没有任何内容可供阅读,你必须输入更多文本才能让它做任何事情。
When you call read()
on the InputStreamReader
, it's allowed to (and often will) read more data from the stream than you've actually requested, and store the rest in a buffer, to satisfy later read
calls. I suspect the whole of the line of text has actually been read by the first InputStreamReader
, so when you construct a second InputStreamReader
for the same stream, there's nothing left for it to read, and you'd have to type in more text to get it to do anything.
这篇关于为什么新的InputStreamReader不会读取控制台中的其余字符?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!