我想要搜索字符串并获取文件中该字符串的值。 例如文件中包含这样的获取给定字符串的值
测试= 1
测试2 = 2
如果搜索字符串str = “测试2” 中给出,那么它应该返回值2。 示例代码我已经试过是
public class ScannerExample {
public static void main(String args[]) throws FileNotFoundException {
//creating File instance to reference text file in Java
String filePath = "c:/temp/test.txt";
//Creating Scanner instnace to read File in Java
String str = "text";
//Reading each line of file using Scanner class
BufferedReader br = new BufferedReader(new FileReader(filePath));
String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
if(sCurrentLine.contains(str)) {
result=true;
System.out.println("Found entry ");
break;
}
}
}
}
我在这里检查,如果值存在与否,请您及时提出了一些方法来获取它的价值
sample.txt:
test=1
test2=2
testnew=new
testold=old2
HashMaps这样的文件?是的.. – Hackerdarshi
你的代码不能编译。 –
你可以添加关于test.txt的信息或发布它吗?每对(如test2 = 2)是否在单独的行中列出? – c0der