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

cha*_*cko 5 java

我有一些像这样的代码:

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(); 
   ...
}
Run Code Online (Sandbox Code Playgroud)

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

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

任何帮助非常感谢.

谢谢,

gpe*_*che 3

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