[英]How to check if a line from txt file startswith “h” in android?
我正在开发一个支持MaterialFileChooser的Livestream应用程序,但是我很难检查所选文本文件中的一行是否以“h”开头,这些行(以h开头)应该存储在一个字符串中。
我试过这个:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("h")) {
// Confusion
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
我不明白你的意思是“应该存储在一个字符串中”。 如果您需要以“h”开头的行,只需创建一个字符串ArrayList
并将其保存在那里。
// Declare an ArrayList first
private ArrayList<String> lineStore = new ArrayList<String>();
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == 1000 && resultCode == RESULT_OK) {
String filePath = data.getStringExtra(FilePickerActivity.RESULT_FILE_PATH);
try {
BufferedReader br = new BufferedReader(new FileReader(filePath));
String line;
while ((line = br.readLine()) != null) {
if (line.startsWith("h")) {
// Store the line in the ArrayList to be used later
lineStore.add(line); // That's what you meant?
}
}
br.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.