与 J2me 读取文件的混淆。请帮助我理解
我正在为 Symbian S60 手机开发 J2ME 应用程序,其中需要读取文本文件。我无法访问 BufferedReader 从文件中提取一行文本,但我确实在诺基亚帮助论坛中找到了这一点,这让我有点困惑。这是代码,我的问题如下。感谢您的回答。
/**
* Reads a single line using the specified reader.
* @throws java.io.IOException if an exception occurs when reading the
* line
*/
private String readLine(InputStreamReader reader) throws IOException {
// Test whether the end of file has been reached. If so, return null.
int readChar = reader.read();
if (readChar == -1) {
return null;
}
StringBuffer string = new StringBuffer("");
// Read until end of file or new line
while (readChar != -1 && readChar != '\n') {
// Append the read character to the string. Some operating systems
// such as Microsoft Windows prepend newline character ('\n') with
// carriage return ('\r'). This is part of the newline character
// and therefore an exception that should not be appended to the
// string.
string.append((char)readChar);
// Read the next character
readChar = reader.read();
}
return string.toString();
}
我的问题是关于 readLine() 方法。在 while() 循环中,为什么我必须检查 readChar != -1 和 != '\n' ?据我了解,-1代表流的结束(EOF)。我的理解是,如果我提取一行,我只需要检查换行符。
谢谢。
I am working on a J2ME app for Symbian S60 phones where reading from a text file is required. I have no access to BufferedReader to extract a line of text from a file, but I did find this in the Nokia help forums, and it has me a bit confused. Here's the code, and my question is below. Thanks for answering.
/**
* Reads a single line using the specified reader.
* @throws java.io.IOException if an exception occurs when reading the
* line
*/
private String readLine(InputStreamReader reader) throws IOException {
// Test whether the end of file has been reached. If so, return null.
int readChar = reader.read();
if (readChar == -1) {
return null;
}
StringBuffer string = new StringBuffer("");
// Read until end of file or new line
while (readChar != -1 && readChar != '\n') {
// Append the read character to the string. Some operating systems
// such as Microsoft Windows prepend newline character ('\n') with
// carriage return ('\r'). This is part of the newline character
// and therefore an exception that should not be appended to the
// string.
string.append((char)readChar);
// Read the next character
readChar = reader.read();
}
return string.toString();
}
My question is regarding the readLine() method. In its while() loop, why must I check that readChar != -1 and != '\n' ? It is my understanding that -1 represents the end of the stream (EOF). I was my understanding that if I am extracting one line, I should only have to check for the newline char.
Thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
请仔细阅读代码文档。你所有的疑问都在这里得到了很好的解答。
该函数正在检查“-1”,因为它正在处理那些没有换行符的流。在这种情况下,它将以字符串形式返回整个流。
Please read the code documentation carefully. All your doubts are well answered in that.
The function is checking the ‘-1’ because it is taking care of those streams which comes without a new line character. In that case it will return the whole stream as a string.
这只是您(喜欢)将逻辑应用于您尝试做/实现的事情的方式。例如,上面的示例可以像他的那样编写,
所以之前的代码示例 readChar 检查 -1 只是安全检查。
It is just way how you (like to) apply logic to what you try to do/achieve. For example above example could have been written like his
So previous code sample readChar check for -1 is just safeguard check.