0

I need read in the loop one text file large.

I tried this solution using set Buffer 1024 * 1024 in the code, but the output of text file in android application is incomplete.

Any idea?

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++;
}
halfer
  • 19,824
  • 17
  • 99
  • 186

1 Answers1

0

What you're doing doesn't make sense at all.

First. You're allocating a huge buffer (1MB) which is not wrong, but not the best choice. You usually have a small buffer (4KB for example) and loop on the input file until you reach the End Of File (EOF), every time you read a string from the file you should append it to a StringBuilder object.

Second. You're reading your string in a variable, then you split it in an array, and then you join it again in a string. What's the point of this?

There are many examples on how to read a text file in Android, like here, here and here.

EDIT:
In those links I placed, there are examples on reading the file line by line so you don't have to search for the newline character. Also if you want your lines in an array you can do so, by removing the code that appends the newly read string to a StringBuilder, and replacing it for the code that adds the newly read string to your array.

Community
  • 1
  • 1
Merlevede
  • 8,140
  • 1
  • 24
  • 39
  • thank you for answer, with this code is populated one spinner in my android form application. I split the text file content for \n but in the spinner the output of list values is truncated, is incomplete. –  Mar 10 '14 at 17:36
  • Ok, I see your suggestion in link's but are not useful in my case, sorry –  Mar 10 '14 at 17:54