簡體   English   中英

如何使用Java中的BufferedReader讀取文件結尾(EOF)?

[英]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 

javadocs

使用文本文件時,使用BufferReader.read()時,EOF可能為-1,char為char。 我用BufferReader.readLine()!= null做了一個測試,它運行正常。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM