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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 23, 20262026-05-23T11:10:21+00:00 2026-05-23T11:10:21+00:00

I’m relatively new to Java and I’m attempting to write a simple android app.

  • 0

I’m relatively new to Java and I’m attempting to write a simple android app. I have a large text file with about 3500 lines in the assets folder of my applications and I need to read it into a string. I found a good example about how to do this but I have a question about why the byte array is initialized to 1024. Wouldn’t I want to initialize it to the length of my text file? Also, wouldn’t I want to use char, not byte? Here is the code:

private void populateArray(){
    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    try {
        inputStream = assetManager.open("3500LineTextFile.txt");
    } catch (IOException e) {
        Log.e("IOException populateArray", e.getMessage());
    }
    String s = readTextFile(inputStream);
    // Add more code here to populate array from string
}

private String readTextFile(InputStream inputStream) {
    ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
    inputStream.length
    byte buf[] = new byte[1024];
    int len;
    try {
        while ((len = inputStream.read(buf)) != -1) {
            outputStream.write(buf, 0, len);
        }
        outputStream.close();
        inputStream.close();
    } catch (IOException e) {
        Log.e("IOException readTextFile", e.getMessage());
    }
    return outputStream.toString();
}

EDIT: Based on your suggestions, I tried this approach. Is it any better? Thanks.

private void populateArray(){
    AssetManager assetManager = getAssets();
    InputStream inputStream = null;
    Reader iStreamReader = null;
    try {
        inputStream = assetManager.open("List.txt");
        iStreamReader = new InputStreamReader(inputStream, "UTF-8");
    } catch (IOException e) {
        Log.e("IOException populateArray", e.getMessage());
    }
    String String = readTextFile(iStreamReader);
    // more code here
}

private String readTextFile(InputStreamReader inputStreamReader) {
    StringBuilder sb = new StringBuilder();
    char buf[] = new char[2048];
    int read;
    try {
        do {
            read = inputStreamReader.read(buf, 0, buf.length);
            if (read>0) {
                sb.append(buf, 0, read);
            }
        } while (read>=0);
    } catch (IOException e) {
        Log.e("IOException readTextFile", e.getMessage());
    }
    return sb.toString();
}
  • 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-23T11:10:22+00:00Added an answer on May 23, 2026 at 11:10 am

    This example is not good at all. It’s full of bad practices (hiding exceptions, not closing streams in finally blocks, not specify an explicit encoding, etc.). It uses a 1024 bytes long buffer because it doesn’t have any way of knowing the length of the input stream.

    Read the Java IO tutorial to learn how to read text from a file.

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

Sidebar

Related Questions

I have just tried to save a simple *.rtf file with some websites and
I have a reasonable size flat file database of text documents mostly saved in
I want use html5's new tag to play a wav file (currently only supported
I have a text area in my form which accepts all possible characters from
I have thousands of HTML files to process using Groovy/Java and I need to
I have a bunch of posts stored in text files formatted in yaml/textile (from
link Im having trouble converting the html entites into html characters, (&# 8217;) i
For some reason, after submitting a string like this Jack’s Spindle from a text
I have a jquery bug and I've been looking for hours now, I can't
I'm new to using the Perl treebuilder module for HTML parsing and can't figure

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.