2014-09-03 32 views
-2

我有一个Java程序在搜索大多数字符串时没有问题,但由于某种原因,我无法找到下面的ina文件,我知道它出现在文件中。我显然试图找到一个具有999的值的特定元素,但我无法这样做。这也适用于其他字符串,但不是下面的字符串。无法在Java文件中查找字符串

for(int i=0;i< inputFile.length;i++) 
     try { 
      br = new BufferedReader(new FileReader(inputFile[i])); 
      try { 
       while((line = br.readLine()) != null) 
       { 
        countLine++; 
        //System.out.println(line); 
        String[] words = line.split(" "); 

        for (String word : words) { 
         if (word.equals(inputSearch)) { 
          count++; 
          countBuffer++; 
         } 
        } 

        if(countBuffer > 0) 
        { 
         countBuffer = 0; 
         lineNumber += countLine + ","; 
        } 

       } 
       br.close(); 
+1

发表你的代码已经有 – 2014-09-03 12:42:06

+0

你怎么搜索?你在使用正则表达式吗? – duffymo 2014-09-03 12:42:27

+0

你确定该文件包含这个字符串吗? – Miki 2014-09-03 12:43:01

回答

1

如果我理解你的问题,你可以使用一个PatternMatcher之类的东西 -

String toMatch = "<element>999</element>"; 
Pattern pattern = Pattern.compile(">\\s*999\\s*<"); 
Matcher match = pattern.matcher(toMatch); 
int count = 0; 
int start = 0; 
while (start < toMatch.length() && match.find(start)) { 
    // Pattern found. 
    start = match.regionEnd() + 1; 
    count++; 
}