简体   繁体   English

在Android上从文本文件读取文件不起作用

[英]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. 更新::我做了一些更改,但是仍然有lines.get(0)的错误,这使我的应用再次崩溃了。

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. 创建一个长度为0的数组,因此您无法访问任何元素。

Using a List<String> would be a better idea, since you don'T know how many lines you'll read. 使用List<String>会是一个更好的主意,因为您不知道会读多少行。

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

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

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

this will allocate memory for you as needed. 这将根据需要为您分配内存。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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