如何查看读者是否在EOF? [英] How to see if a Reader is at EOF?

查看:109
本文介绍了如何查看读者是否在EOF?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的代码需要读入所有文件。目前我正在使用以下代码:

My code needs to read in all of a file. Currently I'm using the following code:

BufferedReader r = new BufferedReader(new FileReader(myFile));
while (r.ready()) {
  String s = r.readLine();
  // do something with s
}
r.close();

如果文件当前为空,那么 s 为null,这不好。是否有 Reader atEOF()方法或等价物?

If the file is currently empty, though, then s is null, which is no good. Is there any Reader that has an atEOF() method or equivalent?

推荐答案

您尝试做的标准模式是:

A standard pattern for what you are trying to do is:

BufferedReader r = new BufferedReader(new FileReader(myFile));
String s = r.readLine();
while (s != null) {
    // do something with s
    s = r.readLine();
}
r.close();

这篇关于如何查看读者是否在EOF?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆