BufferedReader跳过行资产文件java android

时间:2015-12-17 09:15:37

标签: java android file inputstream bufferedreader

我正在尝试读取位于Android项目中我的资源文件夹中的.txt文件。搜索文件,使用InputStreamer读取文件,BufferedReader工作正常,但问题是:它没有读取所有行。因此,当我想在ArrayList中添加一行以供进一步使用时,并非所有行都存在于此列表中。这是我的代码:

InputStream inputStream;
BufferedReader br;
try {
    inputStream = getResources().getAssets().open("KeyMapping.txt");
    br = new BufferedReader(new InputStreamReader(inputStream));
    final ArrayList<String> viewList = new ArrayList<String>();
    String line = null;

    //Add every line (except the first) to an arrayList
    while ((line = br.readLine()) != null && (line = br.readLine()) != "1,2,3,4,5,6,7,8,char") {
        viewList.add(line);
    }

    br.close();
} catch (IOException e) {
    e.printStackTrace();
}

我的.txt文件格式如下:

1,2,3,4,5,6,7,8/char
1,,,,1,,,/a
2,,,,1,,,/b
3,,,,1,,,/c
,1,,,1,,,/d
,2,,,1,,,/e
,3,,,1,,,/f
,,1,,1,,,/g
,,2,,1,,,/h
,,3,,1,,,/i
,,,,1,,,1/j
,,,,1,,,2/k
,,,,1,,,3/l
1,,,,2,,,/m
2,,,,2,,,/n
3,,,,2,,,/o
,1,,,2,,,/p
,2,,,2,,,/q
,3,,,2,,,/r
,,1,,2,,,/s
,,2,,2,,,/t
,,3,,2,,,/u
,,,,2,,,1/v
,,,,2,,,2/w
,,,,2,,,3/x
...

只有少数几行会被添加到ArrayList中,有人知道为什么吗?

1 个答案:

答案 0 :(得分:1)

/etc/puppet/modules/demo/lib/puppet/type

此行从流中读取2行,并始终处理第2行。

此外,您无法将字符串与 while ((line = br.readLine()) != null && (line = br.readLine()) != "1,2,3,4,5,6,7,8,char") 运算符进行比较。使用!=方法。

String.equals()