我正在开发一个支持 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();
}
}
}
关于java - 如何在android中检查txt文件中的一行是否以 "h"开头?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55834990/