简体   繁体   中英

Read File from text file on Android doesn't work

Update :: I have changed my code a bit but still bug with lines.get(0) which makes my app crashed again.

final List<String> lines = new ArrayList<String>();
String line;
int i = 0;

BufferedReader buffreader = null;
try {
    buffreader = new BufferedReader(
            new InputStreamReader(getAssets().open("test.txt")));

    while((line = buffreader.readLine()) != null)
    {
        lines.add(line);
        i++;
    }
} catch (IOException e) {

}

This

final String lines[]= {};

creates an array of length 0, so you can't access any elements.

Using a List<String> would be a better idea, since you don'T know how many lines you'll read.

List<String> lines = new ArrayList<String>();

   // in the loop:
   lines.add( line );

use ArrayList<String> array = new ArrayList<String>();

this will allocate memory for you as needed.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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