Java / Android Read large text file (~2.5 MB) -


i trying read in large text file (~2.5 mb) android application, using 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 decoder. */       return charset.defaultcharset().decode(bb).tostring();   }   {     stream.close();   } } 

i read here effective way read file in java, app crashes force close, believe because of out of memory problem, have code wrapped around try-catch block.

how can read in , not make app crash? have tried several things, result in same, large files. cannot split file up, not acceptable in final application.


update:

here section read file:

try                  {     string str = readfile(filepath);     et.settext(str);     et.setselection(str.length()); } catch (exception ex) {     et.settext("there error reading file: " + filepath + "\ndetails: " + ex); } 

where et autocompletetextview


update 2:

i have run adb , found following:

04-11 15:26:16.805 20646 20658 w activitymanager: activity pause timeout 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 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 activitymanager: process com.ultimatecomputerservicesinc.androidhelloworld (pid 25842) has died. 

what signal 9 mean?


update 3

my suspicions correct, ran out of memory! how can increase 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:    @ android.text.packedintvector.growbuffer(packedintvector.java:257) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.text.packedintvector.insertat(packedintvector.java:187) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.text.dynamiclayout.reflow(dynamiclayout.java:336) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.text.dynamiclayout.<init>(dynamiclayout.java:150) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.widget.textview.makenewlayout(textview.java:4987) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.widget.textview.checkforrelayout(textview.java:5484) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.widget.textview.settext(textview.java:2776) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.widget.textview.settext(textview.java:2644) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.widget.edittext.settext(edittext.java:75) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.widget.textview.settext(textview.java:2619) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ com.ultimatecomputerservicesinc.androidhelloworld.helloworld$1$1.run(helloworld.java:183) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.os.handler.handlecallback(handler.java:587) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.os.handler.dispatchmessage(handler.java:92) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.os.looper.loop(looper.java:143) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ android.app.activitythread.main(activitythread.java:4701) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ java.lang.reflect.method.invokenative(native method) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ java.lang.reflect.method.invoke(method.java:521) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ com.android.internal.os.zygoteinit$methodandargscaller.run(zygoteinit.java:868) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ com.android.internal.os.zygoteinit.main(zygoteinit.java:626) 04-12 20:41:48.905  6610  6610 e androidruntime:    @ dalvik.system.nativestart.main(native method) 

please print exception details in log , post results. this:

catch( exception err) {      log.e("there error reading file", err); } 

this should give more meaningful message explaining happened , trace of methods being executed.

then, might further.

i've had out of memory exceptions when parsed 2.5 mb files dom parser... don't think problem. anyway, solved problem splitting data several files , parsing them 1 @ time. ~1mb "safe" size files.


Comments

Popular posts from this blog

php - What is the difference between $_SERVER['PATH_INFO'] and $_SERVER['ORIG_PATH_INFO']? -

fortran - Function return type mismatch -

queue - mq_receive: message too long -