简体   繁体   English

Java / Android读取大文本文件(〜2.5 MB)

[英]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: 我正在尝试使用以下代码将大型文本文件(〜2.5 MB)读入Android应用程序:

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. 在这里读到,这是在Java中读取文件的最有效方法,但是我的应用程序因强制关闭而崩溃,我相信是因为内存不足问题,因为我将这段代码包裹在try-catch块中。

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 其中et是一个AutoCompleteTextView


UPDATE 2: 更新2:

I have run adb and found the following: 我已经运行adb并发现以下内容:

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? 信号9是什么意思?


UPDATE 3 更新3

My suspicions are correct, I ran out of memory! 我的猜想是正确的,我的记忆力不足了! How can I increase the JVM size on android? 如何增加android上的JVM大小?

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)

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. 将catch(Exception e){}添加到try / finally以捕获异常。

I have an app and I used to load in in 1MB files which wasn't a problem (though a DOM parser was) 我有一个应用程序,我曾经加载过1MB的文件,这不是问题(尽管DOM解析器是)

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. 当我使用DOM解析器解析2.5 MB文件时,我的内存不足异常。但是我不认为您会这样做,所以这可能是另一个问题。 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. 大约1MB是文件的“安全”大小。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM