将字符串与文件中的另一个字符串进行比较

我尝试了“ .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; 
          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
niuhenan 回答:将字符串与文件中的另一个字符串进行比较

if (prefix.trim().equals(pre)) System.out.println("Found"); else System.out.println("Not Found");

将解决问题。

,

prefix = br.readLine()将为您提供完整的单词而不是单词。这意味着您正在将整个行与单词进行比较。您必须检查该行中是否存在必需的单词。

您必须像这样修改

if(pre.indexOf(prefix.trim()) != -1)
             System.out.println("Found");
else  System.out.println("Not Found");
本文链接:https://www.f2er.com/3088765.html

大家都在问