gpt4 book ai didi

java - BufferedReader 跳过行 Assets 文件 java android

转载 作者:行者123 更新时间:2023-11-30 01:42:16 25 4
gpt4 key购买 nike

我正在尝试读取位于我的 Android 项目 Assets 文件夹中的 .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);
}

关于java - BufferedReader 跳过行 Assets 文件 java android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34330737/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com