Java 将字符串与文件中的另一个字符串进行比较
我尝试了.equals、.contains和==我仍然没有得到结果Java 将字符串与文件中的另一个字符串进行比较,java,string,compare,Java,String,Compare,我尝试了.equals、.contains和==我仍然没有得到结果 String pre = "0917"; File file = new File("F:\\Eclipse\\Loading De Luna\\bin\\GlobePrefix.txt"); BufferedReader br = new BufferedReader(new FileReader(file)); String prefix;
String pre = "0917";
File file = new File("F:\\Eclipse\\Loading De Luna\\bin\\GlobePrefix.txt");
BufferedReader br = new BufferedReader(new FileReader(file));
String prefix;
while ((prefix = br.readLine()) != null)
if(prefix.equals(pre))
System.out.println("Found");
else System.out.println("Not Found");
.txt文件包含以下字符串:
0817
0905
0906
0915
0916
0917
0926
0927
0935
0936
0937
0945
0955
0956
0965
prefix=br.readLine将为您提供完整的行,而不是单词。这意味着你要将整行文字与单词进行比较。您必须检查行中是否存在所需的单词
你必须这样修改
if(pre.indexOf(prefix.trim()) != -1)
System.out.println("Found");
else System.out.println("Not Found");
如果前缀为.trim.equalspre
System.out.printlnFound;
其他的
未找到System.out.println
将解决此问题。文件的内容是什么,您希望发生什么,以及会发生什么?发布您期望的输出和您得到的输出。解释您期望输出的原因。我仍然没有得到结果-什么结果?可能尝试像前缀一样修剪。trim.equalspreLooks可能有一些空格,尝试Deadpool建议的方法。