我想知道这有什么问题。它给了我一个构造函数错误(java.io.InputSream)
BufferedReader br = new BufferedReader(System.in);
String filename = br.readLine();
最佳答案
BufferedReader 是装饰另一个阅读器的装饰器。 InputStream 不是读取器。您首先需要一个 InputStreamReader。
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
为了回应您的评论,这里是 readline 的 javadoc:
读行
public String readLine()
throws IOException
Read a line of text. A line is considered to be terminated by any one of a line feed ('\n'), a carriage return ('\r'), or a carriage return followed immediately by a linefeed.
Returns:
A String containing the contents of the line, not including any line-termination characters, or null if the end of the stream has been reached
Throws:
IOException - If an I/O error occurs
要适本地处理此问题,您需要将方法调用放在 try/catch 块中或声明它可以被抛出。
使用 try/catch 块的示例:
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
try{
String filename = br.readLine();
} catch (IOException ioe) {
System.out.println("IO error");
System.exit(1);
}
声明可能抛出异常的示例:
void someMethod() throws IOException {
BufferedReader br = new BufferedReader (new InputStreamReader(System.in));
String filename = br.readLine();
}
关于java - 缓冲阅读器和扫描器,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/5086549/