我需要帮助。
我有一个for
,它检查一些字符串StringToFind
(有时很少甚至没有,有时可能是一百个左右),我需要在文件中找到字符串,如果存在,我需要全线,我需要在Android中使用Eclipse,例如:
示例文件:
Orange, cherry, coconut
Apple, banana, lemon
Lime, pear, grape
我需要什么:
StringToFind = "Apple";
,我需要在文件中找到Apple
(从存储中读取),如果存在Apple
,请将整行换成另一个字符串:StringFound = "Apple, banana, lemon"
。
有人可以帮我吗? 感谢。
答案 0 :(得分:2)
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 "";
}
答案 1 :(得分:0)
//Get the text file
File file = new File("yourFile");
String StringToFind = "Apple";
try {
BufferedReader br = new BufferedReader(new FileReader(file));
String line;
while ((line = br.readLine()) != null) {
if(line.equals(StringToFind));
//Your String matches
}
}
catch (IOException e) {
//You'll need to add proper error handling here
}