java - Android - 在文件中查找字符串,如果存在则使用 eclipse 获取该行

标签 java android eclipse

我需要这方面的帮助。

我有一个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/

相关文章:

java - Kotlin - 无法写入 JSON : Conflicting getter definitions for property (HttpMessageNotWritableException)

android - 选项菜单关闭时调用的方法

java - Android Studio myfirstapp 中的错误 : Gradle errors

java - AgitarOne无法连接到本地主机:8080/agitar-server on eclipse

java - Log4j2 SMTP 附加程序 "Must issue a STARTTLS command first"

java - 如何允许现有正则表达式中的特殊字符集?

java - 用二维数组java写入txt文件

android - 如何以编程方式检测 "Android App"是否在 chrome book 或 Android 手机中运行

java - 当没有未保存的更改时避免关闭编辑器 Eclipse RCP

java - Java 项目和嵌套项目之间的 CXF jar 版本冲突