-2

我正在尝试读取具有 -

你好詹姆斯!

你今天过得怎么样!

我正在使用以下代码:

int readedValue;

do
{   
    while((readedValue = bufferReader.read()) != 10)
   {
    //readedValue = bufferReader.read();
    char ch = (char) readedValue;
    System.out.print(ch);
   } 

}
while ((readedValue = bufferReader.read()) != -1);

当我现在阅读文件时,我得到的输出是 hello James!你今天好吗!

我没有在“如何”中得到“如何”。我怎样才能改变它以获得完整的文本?

4

4 回答 4

1

你在do-while循环的条件下失去了一个角色

do {
    ...
    // ends when the first new line \n character is reached
}
while ((readedValue = bufferReader.read()) != -1);
        ^ never printed 
// the character that isn't read is the first character after the \n, ie. 'H'

使用存储readedValue(readValue) 的单个循环并对该循环进行任何比较。

于 2013-10-02T17:36:26.493 回答
1

I think you need this one...

int readedValue;

while ((readedValue = bufferReader.read()) != -1)
{   
    if(readedValue  != 10)
    {
         System.out.print((char) readedValue);
   } 

}
于 2013-10-02T17:33:55.237 回答
0

这将帮助您:

String line;
int readedValue;
String s = "hello James!\n\rHow are you today!";
StringReader input = new StringReader(s);
BufferedReader lineReader= new BufferedReader (input);

while((line=lineReader.readLine())!=null) {
    StringReader input2 = new StringReader(line);
    BufferedReader charReader= new BufferedReader (input2);
    while((readedValue = charReader.read()) != -1) {
        char ch = (char) readedValue;
        System.out.print(ch);
    }
}
于 2013-10-03T16:20:08.840 回答
0

在您的示例中,当遇到换行符时,您正在读取一个字符两次 ascii 10

do
{   
    while((readedValue = bufferReader.read()) != 10) // Here 
   {
    //readedValue = bufferReader.read();
    char ch = (char) readedValue;
    System.out.print(ch);
   } 

}
while ((readedValue = bufferReader.read()) != -1); // Again here

你应该做的是只读一次

while ((readedValue = bufferReader.read()) != -1)
{   
    if(readedValue != 10) 
   {
    char ch = (char) readedValue;
    System.out.print(ch);
   } 

}
于 2013-10-02T17:37:17.320 回答