我有一些这样的代码:
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()
关于java - 如果我使用 FileReader 读取文件,我怎么知道我在文件中的位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6058135/