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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T15:47:43+00:00 2026-05-17T15:47:43+00:00

I have a file that contains about 200,000 long values that I want to

  • 0

I have a file that contains about 200,000 long values that I want to read as fast as possible into a long[]. It’s for an Android app; function calls are slow (so anything involving reading a long at a time with a “for” loop will be super slow) and I need loading to be fast. What can I use? Everything I look at seems to read only bytes fast.

I’ve used ByteBuffer and FileChannel from the NIO package before and this seems a really fast way to load arrays of values from files. However, I cannot work out how to use this to read data into a long[]. I’ve tried wrapping a long[] as a LongBuffer, but I cannot see any way I can feed data from a file into a LongBuffer.

Edit: Whatever method I use, I need to be able to use Arrays.binarySearch on the long[] array at the end.

  • 1 1 Answer
  • 1 View
  • 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-17T15:47:44+00:00Added an answer on May 17, 2026 at 3:47 pm

    There is no way to cast a byte[] into a long[]. However, you could try to use a FileChannel to read the content into a ByteBuffer and then get hold of a LongBuffer through ByteBuffer.asLongBuffer from which you could get a long[] through LongBuffer.array().

    You could also try FileChannel.map to get a MappedByteBuffer of the file. This may be faster than going through a FileChannel.read.

    If this doesn’t work you could try to use a FileChannel to read the content into a ByteBuffer and then access the longs inside it, using ByteBuffer.getLong(index).


    An alternative solution. (No method-calls in the loop 🙂

    byte[] byteArray = new byte[longCount * 8];
    FileInputStream fis = new FileInputStream("lotsoflongs");
    fis.read(byteArray);
    fis.close();
    for (int i = 0; i < longCount; i += 8)
        longArray[i >> 3] = ((long) byteArray[0+i]        << 56) +
                            ((long)(byteArray[1+i] & 255) << 48) +
                            ((long)(byteArray[2+i] & 255) << 40) +
                            ((long)(byteArray[3+i] & 255) << 32) +
                            ((long)(byteArray[4+i] & 255) << 24) +
                                  ((byteArray[5+i] & 255) << 16) +
                                  ((byteArray[6+i] & 255) <<  8) +
                                  ((byteArray[7+i] & 255) <<  0);
    

    I’ve benchmarked a few solutions now, and this one seems to be the fastest way of doing it. Also, note that the actual bytes read in fis.read(byteArray) may be less than the actual size of byteArray. Thus, if this should be done properly, you need to put it in a loop that iterates until all bytes have been read.

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

Sidebar

Related Questions

I have a text file that contains about 1200-lines. I want to import it
I have an array in perl that contains about 200 indexes that are for
I have a text file that contains about 100000 articles. The structure of file
I have a file that contains some information about the product I work on.
I have a problem. I have an XML file that contains information about 100
I have a file that contains about 1000 lines. The functionality that I am
I have an excel file that contains information about conductors. eg: type r GMR
I have a CSV (comma separated values) file that contains student information. The column
I have a large file that contains world countries/regions that I'm seperating into smaller
I have a text file that contains about a million records. What is the

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.