Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 3355788
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T02:26:13+00:00 2026-05-18T02:26:13+00:00

I have to work with big files (many GB) and need quick lookups to

  • 0

I have to work with big files (many GB) and need quick lookups to retrieve specific lines on request.

The idea has been to maintain a mapping:

some_key -> byte_location

Where the byte location represents where in the file the line starts.

Edit: the question changed a little bit:

First I used:

FileInputStream stream = new FileInputStream(file);
BufferedReader reader = new BufferedReader(new InputStreamReader(stream));
FileChannel channel = stream.getChannel();

I noticed that FileChannel.position() will not return the exact position where the reader is currently reading because it is a “buffered” reader. It reads chunks of a given size (16k here) so what I get from the FileChannel is a multiple of 16k, and not the exact position where the reader is actually reading.

PS: the file is in UTF-8

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-18T02:26:13+00:00Added an answer on May 18, 2026 at 2:26 am

    I would have tried something like this:

        RandomAccessFile raf = new RandomAccessFile(file);
        ...
        raf.seek(position);
        raf.readLine();
        ...
    

    The problem is that readLine() turns each byte into a character with the top 8 bits zero. That’s fine if your file is ASCII or Latin-1, but problematic for UTF-8.

    However, if you are prepare to use RandomAccessFile to write the file, you can use readUTF() and writeUTF() to read and write “lines” encoded as modified UTF-8 Strings.

    FOLLOWUP

    dammit …utf-8 characters are screwed

    Yea … see above.

    Another idea for coping with UTF-8 with RandomAccessFile:

    1. seek to desired position,
    2. use readFully(byte[]) method to read a bunch of bytes into a byte[],
    3. locate pos == position of the end of line in the buffer,
    4. if not found, read more bytes, concatenate and go to step 2.
    5. if found, use new String(bytes, 0, pos, UTF-8) to convert to a Java String.

    This is more cumbersome than using readLine(), but it should be faster than using FileInputStream and skip() when reading multiple lines from the files in random order.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a series of big excel files that work like a program, but
I have a big txt file ,with many strings ,say string-I-want-to-change,now I need to
So I have a big list of integers I have to work with (in
I have a (big) problem that all of you that work with Webforms might
I have to work on a database in which there are many stored-procedures &
I have a work around for this problem, but I've been noticing some peculiar
My config file is really just a big python dict, but I have many
I used the search, and the specific topic has not been covered before to
In the team I'm working in we have a big product with many WCF
The commandlink link2 does not have work after an ajax call is made to

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.