Java / Android Read large text file (~2.5 MB)
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开发者_JAVA技巧.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)
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.
Your app doesn't actually catch any exceptions, it just cleans up resources if it does crash.
Add catch(Exception e ){} to the try/finally to catch the exceptions.
I have an app and I used to load in in 1MB files which wasn't a problem (though a DOM parser was)
精彩评论