Android: read large text file -
i need read in loop 1 text file large.
i tried solution using set buffer 1024 * 1024 in code, output of text file in android application incomplete.
any idea? thank you
u = new url(path); httpurlconnection c = (httpurlconnection) u .openconnection(); c.setrequestmethod("get"); c.connect(); inputstream in = c.getinputstream(); final bytearrayoutputstream bo = new bytearrayoutputstream(); byte[] buffer = new byte[1024 * 1024]; in.read(buffer); bo.write(buffer); string s = bo.tostring(); final vector<string> str = new vector<string>(); string[] line = s.split("\n"); int index = 0; while (index < line.length) { str.add(line[index]); index++; }
what you're doing doesn't make sense @ all.
first. you're allocating huge buffer (1mb) not wrong, not best choice. have small buffer (4kb example) , loop on input file until reach end of file (eof), every time read string file should append stringbuilder object.
second. you're reading string in variable, split in array, , join again in string. what's point of this?
there many examples on how read text file in android, here, here , here.
edit:
in links placed, there examples on reading file line line don't have search newline character. if want lines in array can so, removing code appends newly read string stringbuilder, , replacing code adds newly read string array.
Comments
Post a Comment