我正在寻找一种方法来解析大文件(大约 5-10Go)并尽可能快地搜索一些循环字符串的位置(以字节为单位)。
我尝试通过执行以下操作来使用 RandomAccessFile 阅读器:
RandomAccessFile lecteurFichier = new RandomAccessFile(<MyFile>, "r");
while (currentPointeurPosition < lecteurFichier.length()) {
     char currentFileChar = (char) lecteurFichier.readByte();
     // Test each char for matching my string (by appending chars until I found my string)
     // and keep a trace of all found string's position
}
问题是这段代码太慢了(可能是因为我逐字节读取?)。
我还尝试了下面的解决方案,它在速度方面非常完美,但我无法获得我的字符串的位置。
    FileInputStream is = new FileInputStream(fichier.getFile());
    FileChannel f = is.getChannel();
    ByteBuffer buf = ByteBuffer.allocateDirect(64 * 1024);
    Charset charset = Charset.forName("ISO-8859-1");
    CharsetDecoder decoder = charset.newDecoder();
    long len = 0;
    while ((len = f.read(buf)) != -1) {
        buf.flip();
        String data = "";
        try {
            int old_position = buf.position();
            data = decoder.decode(buf).toString();
            // reset buffer's position to its original so it is not altered:
            buf.position(old_position);
        }
        catch (Exception e) {
            e.printStackTrace();
        }
        buf.clear();
    }
    f.close();
有没有人可以提出更好的解决方案?
提前谢谢你(对不起我的拼写,我是法国人)