Search for a string in large file and save it's position in Java

I'm searching for a way to parse large files (about 5-10Go) and search for position (in byte) of some recurrent strings, the fastest as possible. I've tried to use the RandomAccessFile reader by doing something like bellow: RandomAccessFile lecteurFichier = new RandomAccessFile(, "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 } The problem is this code is too slow (maybe because I read byte by byte ?). I also tried the solution bellow, which is perfect in term of speedness but I can't get my string's positions. 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(); Does anyone has a better solution to propose ? Thank you in advance (and sorry for my spelling, I'm french)
What prevents you from keeping count of bytes read from FileChannel?

以上就是Search for a string in large file and save it's position in Java的详细内容,更多请关注web前端其它相关文章!

赞(0) 打赏
未经允许不得转载:web前端首页 » JavaScript 答疑

评论 抢沙发

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址

前端开发相关广告投放 更专业 更精准

联系我们

觉得文章有用就打赏一下文章作者

支付宝扫一扫打赏

微信扫一扫打赏