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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T17:34:07+00:00 2026-06-02T17:34:07+00:00

How can I read certain number of elements (characters, specifically) at a time in

  • 0

How can I read certain number of elements (characters, specifically) at a time in Java? It’s a little difficult to explain, but my idea is this:

If I have a text file that contains:

This is a text file named text.txt

I want to be able to have a String or a character array of a certain length that iterates through the file. So if I specified the length to be 3, first iteration the char array would contain [T,h,i], and if I iterate through it once, it would become [h,i,s], and then [i,s, ], and so on.

I have tried using the BufferedReader.read(char[], off, len) method which reads certain number of characters at a time from the file, but performance is important for what I’m trying to do.

Is there any method to achieve this in Java? I’ve tried using BufferedReader but I’m not too familiar with it to fully utilize it.

  • 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-06-02T17:34:08+00:00Added an answer on June 2, 2026 at 5:34 pm

    You’ll actually get the best I/O performance by buffering both the input stream and the reader. (Buffering just one gives most of the improvement; double buffering is only a bit better, but it is better.) Here’s sample code to read a file a chunk at a time:

    final int CHUNK_SIZE = 3;
    final int BUFFER_SIZE = 8192; // explicit buffer size is better
    File file = ...
    InputStream is = new BufferedInputStream(new FileInputStream(file), BUFFER_SIZE);
    Reader rdr = new BufferedReader(new InputStreamReader(is), BUFFER_SIZE);
    char buff = new char[CHUNK_SIZE];
    int len;
    while ((len = rdr.read(buff)) != -1) {
       // buff[0] through buff[len-1] are valid
    }
    rdr.close();
    

    This, of course, is missing all sorts of error checking, exception handling, etc., but it shows the basic idea of buffering streams and readers. You may also want to specify a character encoding when constructing the InputStreamReader. (You could bypass dealing with input streams by using a FileReader to start with, but then you cannot specify a character set encoding and lose the slight performance boost that comes from double buffering.)

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

Sidebar

Related Questions

The following method of course works, but after a certain number of uploads (this
I have a file that i need to read a certain number of characters
I can read the MySQL documentation and it's pretty clear. But, how does one
You can read this question where I ask about the best architecture for a
I can read unmanaged memory in C# using UnmanagedMemoryStream, but how can I do
I can read settings like this, for example: final String mytest = System.getString(this.getContentResolver(), System.AIRPLANE_MODE_ON);
I can read XLS file with this code : string path =@c:\r\1.xlsx; OleDbConnection MyConnection
I was just wondering why this certain problem happens to me. If you can
I am creating a file to read in a certain number of .wav files,
First of all, thanks for taking the time to read this. I've been working

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.