简体   繁体   English

如何使用FileInputStream和BufferReader实现RandomAccessFile的seek()和getFilePointer()?

[英]How to implement RandomAccessFile's seek() and getFilePointer() using FileInputStream and BufferReader?

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. 我想打开一个包含UTF-8编码文本的文件,可以设置位置,可以读取25行,可以获取位置。

Unfortunately RandomAccessFile does not support UTF-8 encoding. 不幸的是RandomAccessFile不支持UTF-8编码。 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. 我用返回的偏移值递归调用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;
}

how can I get last offset correct so I can print 1-25, 26-50, 51-75. 我怎样才能正确地获得上一个偏移量,以便可以打印1-25、26-50、51-75。 76-100 and so on. 76-100等。

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. 是的。为了获得向后兼容性,我有一些约束,我必须使用readLine()逐行读取文本文件,并且一次只能读取25-25行,而不是整个文本。

The BufferedReader reads ahead,so the position on the underlying file > the logical position the BufferedReader has reached. BufferedReader会预先读取,因此基础文件上的位置> BufferedReader达到的逻辑位置。 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. 您可以尝试使用DataInputStream.readLine(),它不支持,不推荐使用,并且不能处理Javadoc中针对不推荐使用的问题。

It does support utf. 它确实支持utf。 please refer this url: http://www.tutorialspoint.com/java/io/randomaccessfile_seek.htm 请参考以下网址: http : //www.tutorialspoint.com/java/io/randomaccessfile_seek.htm

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). 如果必须这样做,则可以使用BufferedReader.skip方法跳过字符(保留已读取的字符数的计数)。 But if possible, I'd suggest to just keep the same BufferedReader around (without closing it), so that it's automatically in the right place in any case. 但是,如果可能的话,我建议保持相同的BufferedReader (而不关闭它),以便在任何情况下都自动将其放在正确的位置。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM