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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T22:12:33+00:00 2026-05-28T22:12:33+00:00

When given a buffer of MAX_BUFFER_SIZE, and a file that far exceeds it, how

  • 0

When given a buffer of MAX_BUFFER_SIZE, and a file that far exceeds it, how can one:

  1. Read the file in blocks of MAX_BUFFER_SIZE?
  2. Do it as fast as possible

I tried using NIO

    RandomAccessFile aFile = new RandomAccessFile(fileName, "r");
    FileChannel inChannel = aFile.getChannel();

    ByteBuffer buffer = ByteBuffer.allocate(CAPARICY);

    int bytesRead = inChannel.read(buffer);

    buffer.flip();

        while (buffer.hasRemaining()) {
            buffer.get();
        }

        buffer.clear();
        bytesRead = inChannel.read(buffer);

    aFile.close();

And regular IO

    InputStream in = new FileInputStream(fileName);

    long length = fileName.length();

    if (length > Integer.MAX_VALUE) {
        throw new IOException("File is too large!");
    }

    byte[] bytes = new byte[(int) length];

    int offset = 0;

    int numRead = 0;

    while (offset < bytes.length
            && (numRead = in.read(bytes, offset, bytes.length - offset)) >= 0) {
        offset += numRead;
    }

    if (offset < bytes.length) {
        throw new IOException("Could not completely read file " + fileName);
    }

    in.close();

Turns out that regular IO is about 100 times faster in doing the same thing as NIO. Am i missing something? Is this expected? Is there a faster way to read the file in buffer chunks?

Ultimately i am working with a large file i don’t have memory for to read it all at once. Instead, I’d like to read it incrementally in blocks that would then be used for processing.

  • 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-28T22:12:34+00:00Added an answer on May 28, 2026 at 10:12 pm

    Assuming that you need to read the entire file into memory at once (as you’re currently doing), neither reading smaller chunks nor NIO are going to help you here.

    In fact, you’d probably be best reading larger chunks – which your regular IO code is automatically doing for you.

    Your NIO code is currently slower, because you’re only reading one byte at a time (using buffer.get();).

    If you want to process in chunks – for example, transferring between streams – here is a standard way of doing it without NIO:

    InputStream is = ...;
    OutputStream os = ...;
    
    byte buffer[] = new byte[1024];
    int read;
    while((read = is.read(buffer)) != -1){
        os.write(buffer, 0, read);
    }
    

    This uses a buffer size of only 1 KB, but can transfer an unlimited amount of data.

    (If you extend your answer with details of what you’re actually looking to do at a functional level, I could further improve this to a better answer.)

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

Sidebar

Related Questions

Given this piece of code : public static void writeFile(File file,List buffer)throws IOException{ File
Given the URL (single line): http://test.example.com/dir/subdir/file.html How can I extract the following parts using
How do you yank all matching lines into a buffer? Given a file like:
The code given below reads file contents to buffer, and then does something with
I have function in C that reads byte by byte from a given buffer
Does Java have buffer overflows? If yes can you give me scenarios?
Given a (source) patch file, what's the easiest way to apply this patch on
I am trying to encode series of images to one video file. I am
I have a string buffer of a huge text file. I have to search
I need to know how big a given in-memory buffer will be as an

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.