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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 21, 20262026-05-21T06:38:59+00:00 2026-05-21T06:38:59+00:00

I am trying to read in a large text file (~2.5 MB) into my

  • 0

I am trying to read in a large text file (~2.5 MB) into my Android application, using the following code:

private static String readFile(String path) throws IOException {
  FileInputStream stream = new FileInputStream(new File(path));
  try {
      FileChannel fc = stream.getChannel();
      MappedByteBuffer bb = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());
      /* Instead of using default, pass in a decoder. */
      return Charset.defaultCharset().decode(bb).toString();
  }
  finally {
    stream.close();
  }
}

I read here that this is the most effective way to read a file in java, but my app crashes with a force close, I believe because of an out of memory problem, as I have this code wrapped around a try-catch block.

How can I read this in and not make the app crash? I have tried several things, all result in the same, but only for large files. I cannot split the file up, that will not be acceptable in the final application.


UPDATE:

Here is the section where I actually read the file:

try                 
{
    String str = readFile(filePath);
    et.setText(str);
    et.setSelection(str.length());
}
catch (Exception ex)
{
    et.setText("There was an error reading the file: " + filePath + "\nDetails: " + ex);
}

Where et is an AutoCompleteTextView


UPDATE 2:

I have run adb and found the following:

04-11 15:26:16.805 20646 20658 W ActivityManager: Activity pause timeout for HistoryRecord{45b37620 com.ultimatecomputerservicesinc.androidhelloworld/.HelloWorld}
04-11 15:26:17.032 20727 20727 D dalvikvm: GC_EXTERNAL_ALLOC freed 1202 objects / 51792 bytes in 66ms
-- snip --
04-11 15:26:20.868 20727 20727 D dalvikvm: GC_EXTERNAL_ALLOC freed 639 objects / 25048 bytes in 38ms
04-11 15:26:20.961 25842 25842 I Process : Sending signal. PID: 25842 SIG: 9
04-11 15:26:21.102 20727 20727 D dalvikvm: GC_EXTERNAL_ALLOC freed 626 objects / 24328 bytes in 93ms
04-11 15:26:21.141 20646 20650 I ActivityManager: Process com.ultimatecomputerservicesinc.androidhelloworld (pid 25842) has died.

What does signal 9 mean?


UPDATE 3

My suspicions are correct, I ran out of memory! How can I increase the JVM size on android?

04-12 20:41:48.905  6610  6610 E AndroidRuntime: FATAL EXCEPTION: main
04-12 20:41:48.905  6610  6610 E AndroidRuntime: java.lang.OutOfMemoryError
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.text.PackedIntVector.growBuffer(PackedIntVector.java:257)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.text.PackedIntVector.insertAt(PackedIntVector.java:187)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.text.DynamicLayout.reflow(DynamicLayout.java:336)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.text.DynamicLayout.<init>(DynamicLayout.java:150)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.widget.TextView.makeNewLayout(TextView.java:4987)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.widget.TextView.checkForRelayout(TextView.java:5484)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.widget.TextView.setText(TextView.java:2776)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.widget.TextView.setText(TextView.java:2644)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.widget.EditText.setText(EditText.java:75)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.widget.TextView.setText(TextView.java:2619)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at com.ultimatecomputerservicesinc.androidhelloworld.HelloWorld$1$1.run(HelloWorld.java:183)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.os.Handler.handleCallback(Handler.java:587)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.os.Handler.dispatchMessage(Handler.java:92)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.os.Looper.loop(Looper.java:143)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at android.app.ActivityThread.main(ActivityThread.java:4701)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at java.lang.reflect.Method.invokeNative(Native Method)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at java.lang.reflect.Method.invoke(Method.java:521)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:868)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:626)
04-12 20:41:48.905  6610  6610 E AndroidRuntime:    at dalvik.system.NativeStart.main(Native Method)
  • 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-21T06:38:59+00:00Added an answer on May 21, 2026 at 6:38 am

    Please do print the exception details in the log and post the results. Do something like this:

    catch( Exception err) {
    
        Log.e("There was an error reading the file", err);
    }
    

    This should give you some more meaningful message explaining what happened and also a trace of the methods that were being executed.

    Then, we might help you further.

    I’ve had out of memory exceptions when I parsed 2.5 MB files with a DOM parser… but I don’t think you do it so this could be another problem. Anyway, I solved my problem by splitting the data into several files and parsing them one at a time. ~1MB was a “safe” size for the files.

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

Sidebar

Related Questions

I'm trying to read a .doc file into a database so that I can
Edit: I'm trying to convert a text file into bytes. I'm not sure if
I am trying to read a large list of English words from a text
I'm trying to read some large text files (between 50M-200M), doing simple text replacement
I am trying to read large files lets say illustrator file or photoshop file
I'm trying to import a csv into a recordset. I've been using code kindly
I'm trying to process a large text file through a HttpServlet (tomcat). As this
i've been trying to read a large file in haskell. I need to compress
I'm currently trying to read in an XML file, make some minor changes (alter
I am trying to read a single file from a java.util.zip.ZipInputStream , and copy

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.