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 6244899
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 24, 20262026-05-24T12:24:39+00:00 2026-05-24T12:24:39+00:00

I have a binary file (about 100 MB) that I need to read in quickly.

  • 0

I have a binary file (about 100 MB) that I need to read in quickly. In C++ I could just load the file into a char pointer and march through it by incrementing the pointer. This of course would be very fast.

Is there a comparably fast way to do this in Java?

  • 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-24T12:24:40+00:00Added an answer on May 24, 2026 at 12:24 pm

    If you use a memory mapped file or regular buffer you will be able to read the data as fast your hardware allows.

    File tmp = File.createTempFile("deleteme", "bin");
    tmp.deleteOnExit();
    int size = 1024 * 1024 * 1024;
    
    long start0 = System.nanoTime();
    FileChannel fc0 = new FileOutputStream(tmp).getChannel();
    ByteBuffer bb = ByteBuffer.allocateDirect(32 * 1024).order(ByteOrder.nativeOrder());
    
    for (int i = 0; i < size; i += bb.capacity()) {
        fc0.write(bb);
        bb.clear();
    }
    long time0 = System.nanoTime() - start0;
    System.out.printf("Took %.3f ms to write %,d MB using ByteBuffer%n", time0 / 1e6, size / 1024 / 1024);
    
    long start = System.nanoTime();
    FileChannel fc = new FileInputStream(tmp).getChannel();
    MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, size);
    LongBuffer longBuffer = buffer.order(ByteOrder.nativeOrder()).asLongBuffer();
    long total = 0; // used to prevent a micro-optimisation.
    while (longBuffer.remaining() > 0)
        total += longBuffer.get();
    fc.close();
    long time = System.nanoTime() - start;
    System.out.printf("Took %.3f ms to read %,d MB MemoryMappedFile%n", time / 1e6, size / 1024 / 1024);
    
    long start2 = System.nanoTime();
    FileChannel fc2 = new FileInputStream(tmp).getChannel();
    bb.clear();
    while (fc2.read(bb) > 0) {
        while (bb.remaining() > 0)
            total += bb.get();
        bb.clear();
    }
    fc2.close();
    long time2 = System.nanoTime() - start2;
    System.out.printf("Took %.3f ms to read %,d MB File via NIO%n", time2 / 1e6, size / 1024 / 1024);
    

    prints

    Took 305.243 ms to write 1,024 MB using ByteBuffer
    Took 286.404 ms to read 1,024 MB MemoryMappedFile
    Took 155.598 ms to read 1,024 MB File via NIO
    

    This is for a file 10x larger than what you want. Its this fast because the data is being cached in memory (and I have an SSD drive). If you have fast hardware, the data can be read pretty fast.

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

Sidebar

Related Questions

I have binary data in a file that I can read into a byte
I just have a question about binary file in an iPhone app. When an
I have a binary file with variable length record that looks about like this:
I have a binary file that was created on a unix machine. It's just
I have a binary file that I need to insert a header at the
I have a binary file that I have to parse and I'm using Python.
If I have a large binary file (say it has 100,000,000 floats), is there
So here's my issue. I have a binary file that I want to edit.
I have a large binary file to parse, and i am not sure about
I have a text file with about 100,000 lines (5 MB), which is updated

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.