我需要这方面的帮助。
我有一个for
,它检查一些字符串StringToFind
(有时很少甚至没有,有时可能有数百个左右),我需要找到该字符串在一个文件中,如果存在,我需要获取整行,我需要使用 Eclipse 在 Android 中做一些事情,例如:
示例文件:
Orange, cherry, coconut
Apple, banana, lemon
Lime, pear, grape
我需要什么:
StringToFind = "Apple";
,我需要在文件中查找 Apple
(从存储中读取),如果 Apple
存在,将整行内容放入另一个字符串中:StringFound = "Apple,banana,lemon"
.
有人可以帮我吗? 谢谢。
最佳答案
private String getStringFromFile(String matching, File inFile) throws IOException {
BufferedReader br = new BufferedReader(new FileReader(inFile));
String line;
while ((line = br.readLine()) != null) {
if (line.contains(matching)) {
br.close();
return line;
}
}
br.close();
return "";
}
关于java - Android - 在文件中查找字符串,如果存在则使用 eclipse 获取该行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21090883/