![](/img/trans.png)
[英]Dealing with end of 'file' using BufferedReader.read()
[英]How to read until end of file (EOF) using BufferedReader in Java?
我在閱讀輸入時EOF
問題,直到Java
EOF
。 在這里,有單輸入,輸出考慮每行的輸入。
例:
輸入:
1
2
3
4
5
輸出:
0
1
0
1
0
但是,我使用Java進行編碼,當我輸入兩個數字時,將打印單個輸出。 我希望使用Java中的BufferedReader
單行輸入並打印單行輸出(終止EOF
)。
這是我的代碼:
BufferedReader input = new BufferedReader(new InputStreamReader(System.in));
StringBuffer pr = new StringBuffer("");
String str = "";
while((str=input.readLine())!=null && str.length()!=0) {
BigInteger n = new BigInteger(input.readLine());
}
你正在消耗一條線,它被丟棄了
while((str=input.readLine())!=null && str.length()!=0)
並且閱讀一篇文章
BigInteger n = new BigInteger(input.readLine());
所以嘗試從字符串中獲取bigint
BigInteger n = new BigInteger(str);
Constructor used: BigInteger(String val)
Aslo改變while((str=input.readLine())!=null && str.length()!=0)
to
while((str=input.readLine())!=null)
看到相關的帖子字符串到bigint
readLine()
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
使用文本文件時,使用BufferReader.read()時,EOF可能為-1,char為char。 我用BufferReader.readLine()!= null做了一個測試,它運行正常。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.