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

  • Home
  • SEARCH
  • 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 7581119
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T18:07:18+00:00 2026-05-30T18:07:18+00:00

I want to read a text file and store its contents in an array

  • 0

I want to read a text file and store its contents in an array where each element of the array holds up to 500 characters from the file (i.e. keep reading 500 characters at a time until there are no more characters to read).

I’m having trouble doing this because I’m having trouble understanding the difference between all of the different ways to do IO in Java and I can’t find any that performs the task I want.

And will I need to use an array list since I don’t initially know how many items are in the array?

  • 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-30T18:07:19+00:00Added an answer on May 30, 2026 at 6:07 pm

    It would be hard to avoid using ArrayList or something similar. If you know the file is ASCII, you could do

    int partSize = 500;
    File f = new File("file.txt");
    String[] parts = new String[(f.length() + partSize - 1) / partSize];
    

    But if the file uses a variable-width encoding like UTF-8, this won’t work. This code will do the job.

    static String[] readFileInParts(String fname) throws IOException {
        int partSize = 500;
        FileReader fr = new FileReader(fname);
        List<String> parts = new ArrayList<String>();
        char[] buf = new char[partSize];
        int pos = 0;
    
        for (;;) {
            int nRead = fr.read(buf, pos, partSize - pos);
            if (nRead == -1) {
                if (pos > 0)
                    parts.add(new String(buf, 0, pos));
                break;
            }
            pos += nRead;
            if (pos == partSize) {
                parts.add(new String(buf));
                pos = 0;
            }
        }
        return parts.toArray(new String[parts.size()]);
    }
    

    Note that FileReader uses the platform default encoding. To specify a specific encoding, replace it with new InputStreamReader(new FileInputStream(fname), charSet). It bit ugly, but that’s the best way to do it.

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

Sidebar

Related Questions

I want to read each line from a text file and store them in
i want to read text file containing the data of 4X4 matrix each element
In C++, I want to sequentially read word from a text file, and store
I want to read graph adjacency information from a text file and store it
I want to be able to read from an unsorted source text file (one
I want to be able to read a text file for its rows and
I'm trying to read a text file and store each line in a node
I just want to read a text file and store the data into a
I have a text file. I want read that file. But In that if
I added a text file to a testapp's solution and I want to read

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.