android - Opening a 12kb text file takes WAY too long....? -


the following code works, takes way long (over minute) open small file. logcat shows lot of instances of "gc_for_malloc freed #### objects / ###### bytes in ##ms". suggestions?

 file dirpath = new file(environment.getexternalstoragedirectory(), "myfolder");  string content = getfile("test.txt");   public string getfile(string file){   string content = "";   try {    file dirpathfile = new file(dirpath, file);    fileinputstream fis = new fileinputstream(dirpathfile);    int c;    while((c = fis.read()) != -1) {     content += (char)c;    }    fis.close();   } catch (exception e) {    getlog("error (" + e.tostring() + ") with: " + file);   }   return content;  } 

update:

this looks now:

file dirpath = new file(environment.getexternalstoragedirectory(), "myfolder"); string content = getfile("test.txt");  public string getfile(string file){     string content = "";     file dirpathfile = new file(dirpath, file);     try {         stringbuilder text = new stringbuilder();         bufferedreader br = new bufferedreader(new filereader(dirpathfile));         string line;         while ((line = br.readline()) != null) {             text.append(line);             text.append('\n');         }         content = new string(text);         } catch (exception e) {             getlog("error (" + e.tostring() + ") with: " + file);     }     return content; } 

thank all!!

using += on string extremely inefficient - allocate , deallocate memory, need avoid!

if need add characters, use stringbuilder , give sufficiently big buffer front.

however, it's better read entire file byte array , create string byte array. use string(byte[]) constructor.


Comments

Popular posts from this blog

asp.net - repeatedly call AddImageUrl(url) to assemble pdf document -

java - Android recognize cell phone with keyboard or not? -

iphone - How would you achieve a LED Scrolling effect? -