加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 编程开发 > Java > 正文

如果我用FileReader读取它,我怎么知道我在文件中的位置?

发布时间:2020-12-14 19:14:29 所属栏目:Java 来源:网络整理
导读:我有一些像这样的代码: FileReader fr = new FileReader(file);BufferedReader reader = new BufferedReader(fr);for (int i=0;i 我无法弄清楚如何获得/计算位置的值. 两件事情: 我显然没有固定长度的文件(即:每行有不同的长度) 我需要让它适用于任何系统

我有一些像这样的代码:

FileReader fr = new FileReader(file);
BufferedReader reader = new BufferedReader(fr);
for (int i=0;i<100;i++) 
{
   String line = reader.readLine(); 
   ...
}

// at this point I would like to know where I am in the file.
// let's assign that value to 'position' 
// then I would be able to continue reading the next 100 lies (this could be done later on ofcourse... )
// by simply doing this: 

FileReader fr = new FileReader(file);
fr.skip(position); 
BufferedReader reader = new BufferedReader(fr);
for (int i=0;i<100;i++) 
{
   String line = reader.readLine(); 
   ...
}

我无法弄清楚如何获得/计算’位置’的值.

两件事情:
我显然没有固定长度的文件(即:每行有不同的长度)
我需要让它适用于任何系统(linux,unix,windows),所以我不确定我是否可以假设换行的长度(无论是一个还是两个字符)

任何帮助非常感谢.

谢谢,

最佳答案
如果您可以保持文件打开,可以尝试使用mark()和reset().如果必须关闭该文件并重新打开,请尝试FileInputStream.getChannel().position()和FileInputStream.skip()

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读