如何检查 txt 文件中的一行是否以 android 中的 "h" 开头?
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();
}
}
}
我不明白"should be stored in a string"是什么意思。如果您需要以 "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();
}
}
}
我正在开发一个支持 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();
}
}
}
我不明白"should be stored in a string"是什么意思。如果您需要以 "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();
}
}
}