如何清除/重置/打开输入流,以便它可以在 Java 中的 2 种不同方法中使用?
这是代码:
package testpack;
import java.io.*;
public class InputStreamReadDemo {
private void readByte() throws IOException {
System.out.print("Enter the byte of data that you want to be read: ");
int a = System.in.read();
System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
// tried writing System.in.close(); and System.in.reset();
}
private void readLine() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line of text: ");
String res = br.readLine();
System.out.println("You entered the following line of text: "+res);
// ... and tried writing br.close();
// but that messes things up since the input stream gets closed and reset..
}
public static void main(String[] args) throws IOException {
InputStreamReadDemo isrd = new InputStreamReadDemo();
isrd.readByte(); // method 1
isrd.readLine(); // method 2
}
}
这是我运行此命令时的输出(调用第一个方法时我输入“Something”,然后第二个方法出于某种原因自动读取“Something”,而不是让我再次输入......):
run:
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: You entered the following line of text: omething
BUILD SUCCESSFUL (total time: 9 seconds)
Here's the code:
package testpack;
import java.io.*;
public class InputStreamReadDemo {
private void readByte() throws IOException {
System.out.print("Enter the byte of data that you want to be read: ");
int a = System.in.read();
System.out.println("The first byte of data that you inputted corresponds to the binary value "+Integer.toBinaryString(a)+" and to the integer value "+a+".");
// tried writing System.in.close(); and System.in.reset();
}
private void readLine() throws IOException {
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Enter a line of text: ");
String res = br.readLine();
System.out.println("You entered the following line of text: "+res);
// ... and tried writing br.close();
// but that messes things up since the input stream gets closed and reset..
}
public static void main(String[] args) throws IOException {
InputStreamReadDemo isrd = new InputStreamReadDemo();
isrd.readByte(); // method 1
isrd.readLine(); // method 2
}
}
Here's the output when I run this (and I input "Something" when the first method is called, the second method then auto-reads "omething" for some reason instead of letting me input again..):
run:
Enter the byte of data that you want to be read: Something
The first byte of data that you inputted corresponds to the binary value 1010011 and to the integer value 83.
Enter a line of text: You entered the following line of text: omething
BUILD SUCCESSFUL (total time: 9 seconds)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
它打印出“something”的原因是,在调用 readByte 时,您只消耗输入的第一个字节。然后 readLine 消耗流中剩余的字节。
在 readByte 方法的末尾尝试这一行:
System.in.skip(System.in.available());
这将跳过流中的剩余字节(“something”),同时仍然保持流打开以使用 readLine 的下一个输入。
关闭 System.in 或 System.out 通常不是一个好主意。
结果:
更新: 正如我们在聊天中讨论的那样,它可以在命令行中运行,但不能在 netbeans 中运行。一定是IDE的东西。
The reason it prints out "omething" is that in your call to readByte, you only consume the first byte of your input. Then readLine consumes the remainder of the bytes in the stream.
Try this line at the end of your readByte method:
System.in.skip(System.in.available());
That will skip over the remaining bytes in your stream ("omething") while still leaving your stream open to consume your next input for readLine.
Its generally not a good idea to close System.in or System.out.
Results in:
UPDATE: As we discussed in chat, it works from the command line, but not in netbeans. Must be something with the IDE.