我正在尝试读取一个文本文件,其中包含 -
hello James!
How are you today!
我想读取字符串中的每个字符,直到找到 EOL 字符。因为我使用的是 Windows,其中 /n/r 代表 EOL 字符。我如何编写一个条件来遍历字符串的所有字符并打印它们一一直到达到 EOL(/n/r)。
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!how are you Today!
我没有得到“How”中的“How”。我可以改变什么以获得完整的文本?
正如人们所指出的,readline()
方法读取下一行分隔符,并返回删除分隔符的行。所以你的测试'\n'
and '\r'
in line
不可能评估为true
.
But you can easily add an extra end-of-line when you output the line
string1.
1 - that is, unless you actually need to preserve the exact same end-of-line sequence characters as in the input stream.
You ask:
而不是使用readline()
,有什么方法可以使用缓冲区读取器读取每个字符并打印它们吗?
好,当然。这read()
方法返回一个字符或-1
来表示EOF。所以:
int ch = br.read();
while (ch != -1) {
System.out.print((char) ch);
ch = br.read();
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)