BufferedReader 跳过行资产文件 java android

BufferedReader skip lines assets file java android

我正在尝试读取位于 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 中只会添加其中几行,有人知道为什么吗?

 while ((line = br.readLine()) != null && (line = br.readLine()) != "1,2,3,4,5,6,7,8,char") 

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

此外,您不能使用 != 运算符比较字符串。使用String.equals()方法。

while ((line = br.readLine()) != null)
{
 if(line.equals("1,2,3,4,5,6,7,8,char"))
   continue;
 //add if it's not that string
 viewList.add(line);
}