2013-08-01 73 views
10

我有读取输入问题,直到EOFJava。在这里,有单个输入和输出考虑输入每一行。如何在Java中使用BufferedReader读取文件结尾(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()); 
} 
+2

你可以粘贴你的代码试图帮助你吗? – Deckard27

+0

大概'br.readLine()!= null' !!! – NINCOMPOOP

+0

粘贴你的代码?你对 –

回答

13

你是消费在一行,并抛弃

while((str=input.readLine())!=null && str.length()!=0) 

,并在

BigInteger n = new BigInteger(input.readLine()); 

阅读BIGINT所以要尽量获得BIGINT来自读取为

的字符串
BigInteger n = new BigInteger(str); 

    Constructor used: BigInteger(String val) 

ASLO变化while((str=input.readLine())!=null && str.length()!=0)

while((str=input.readLine())!=null) 

见相关post string to 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

+0

谢谢,但EOF怎么样? –

+0

EOF将在'(str = input.readLine())!= null'上被检测到。 –

+0

哦,我的上帝。谢谢,它的作品,我可以更多地了解你告诉我的线条。谢谢,再次感谢你,@问题 –

7

对于文本文件,也许EOF为-1使用BufferReader.read(),字符时由char。 我使用BufferReader.readLine()!= null进行了测试,并且它正常工作。

相关问题