如何使用FileInputStream和BufferReader实现RandomAccessFile的seek()和getFilePointer()?
我想打开一个包含UTF-8编码文本的文件,可以设置位置,读取25行,可以获取位置。
不幸的是RandomAccessFile不支持UTF-8编码。所以我写了这段代码。我使用返回的偏移值递归调用 getParsedLines() 方法来读取接下来的 25 行,但它会打印前 1-25 行,然后打印 349-373、695-719 等。
public long getParsedLines(File file, long offset) {
int counter = 0;
FileInputStream fis = null;
InputStreamReader streamReader = null;
BufferedReader br = null;
try {
fis = new FileInputStream(file);
fis.getChannel().position(offset);
streamReader = new InputStreamReader(fis, "UTF8");
br = new BufferedReader(streamReader);
String str;
while (counter <= 24) {
if ((str = br.readLine()) != null) {
System.out.println(str);
} else {
offset = -1;
break;
}
counter++;
}
if (fis != null) {
offset = fis.getChannel().position();
}
} catch (IOException ex) {
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
}
}
return offset;
}
如何获得正确的最后偏移量,以便可以打印 1-25、26-50、51-75。 76-100 等等。
是的..为了向后兼容,我有一些限制,我必须使用 readLine() “逐行”读取文本文件,并且一次只能读取 25-25 行,而不是全文。
I would like to Open a file containing UTF-8 encoded text, Can able to set position, Read 25 lines, Can able to get position.
Unfortunately RandomAccessFile does not support UTF-8 encoding. So I have written this code. I recursive call getParsedLines() method with the returned offset value to read next 25 lines but it prints first 1-25 rows, then print 349-373, 695-719 and so on.
public long getParsedLines(File file, long offset) {
int counter = 0;
FileInputStream fis = null;
InputStreamReader streamReader = null;
BufferedReader br = null;
try {
fis = new FileInputStream(file);
fis.getChannel().position(offset);
streamReader = new InputStreamReader(fis, "UTF8");
br = new BufferedReader(streamReader);
String str;
while (counter <= 24) {
if ((str = br.readLine()) != null) {
System.out.println(str);
} else {
offset = -1;
break;
}
counter++;
}
if (fis != null) {
offset = fis.getChannel().position();
}
} catch (IOException ex) {
} finally {
try {
if (fis != null) {
fis.close();
}
} catch (IOException ex) {
}
}
return offset;
}
how can I get last offset correct so I can print 1-25, 26-50, 51-75. 76-100 and so on.
Yes.. For some backward compatibility I have some constraint that I have to read text file "line by line" using readLine() and read only 25-25 lines at a time not the whole text.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
BufferedReader 会提前读取,因此底层文件的位置 > BufferedReader 已到达的逻辑位置。您可以尝试 DataInputStream.readLine(),但它不会,并且已被弃用,并且它不能处理 Javadoc 中针对弃用提到的问题。
The BufferedReader reads ahead,so the position on the underlying file > the logical position the BufferedReader has reached. You could try DataInputStream.readLine(), which doesn't, and which is deprecated, and which doesn't handle the issues mentioned in the Javadoc against the deprecation.
如果必须这样做,可以使用 BufferedReader.skip 方法跳过字符(保留已读取的字符数)。但如果可能的话,我建议保留相同的 BufferedReader(不关闭它),以便在任何情况下它都会自动位于正确的位置。
If you must do it this way, you can use the
BufferedReader.skip
method to skip characters (having kept a count of the number of chars already read). But if possible, I'd suggest to just keep the sameBufferedReader
around (without closing it), so that it's automatically in the right place in any case.它确实支持utf。
请参考此网址:http://www.tutorialspoint.com/java/io/randomaccessfile_seek。嗯
It does support utf.
please refer this url: http://www.tutorialspoint.com/java/io/randomaccessfile_seek.htm