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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T15:33:42+00:00 2026-06-14T15:33:42+00:00

How can I read 20 characters of a text file progressively, for example, If

  • 0

How can I read 20 characters of a text file progressively, for example, If i have a function read_next, calling it the first time would return the first 20 characters in a string, calling it the second time would return the next 20 characters of the file. Please note, I don’t want to read the whole file into an array then break it up.

  • 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-14T15:33:44+00:00Added an answer on June 14, 2026 at 3:33 pm

    Basically, you want to use InputStream#read(byte[])

    Reads some number of bytes from the input stream and stores them into
    the buffer array b. The number of bytes actually read is returned as
    an integer

    public int read(InputStream is, byte[] bytes) throws IOException {
        return is.read(bytes);
    }
    

    Then you basically want to call this method…

    byte[] bytes = new byte[20];
    int bytesRead = -1;
    while ((bytesRead = read(is, bytes)) != -1) {
        // Process the bytes..
        // Note, while bytes.length will always == 20
        // there will only ever be bytesRead worth of 
        // values in the array
    }
    

    UPDATED

    After some nice feedback, you could also apply the same idea to UFT-8 encoded file using a Reader

    public int read(Reader reader, char[] chars) throws IOException {
        return reader.read(chars);
    }
    

    And call the method as such…

    Reader reader = new InputStreamReader(new FileInputStream("file"), "UTF-8");
    char[] chars = new char[20];
    int charsRead = -1;
    while ((charsRead = read(reader, chars)) != -1) {
        // Process chars, the same caveats apply as above...
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

How can I read from a text file, numbers that only consists of one
I have the following basic code to read a text file from a StreamReader:
I would like to read a text file into an array of strings using
I have a text file that contains a list of manufacturers. How can I
I have a text file which has first line as below: j0W82LBrSdUbw Basically it
i needed to create a program that would read a text file and count
I have a text file in this format: abc? cdfde nhj.cde' dfwe-df$sde..... How can
Is there any tool which can read a text file and insert the records
I'm trying Perl on Mac. I have to read an RTF text file. the
Hello I have a text file which I am trying to read a set

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.