Я пытался найти это полное слово "(Ошибка: 87)" в моем текстовом файле.Я использовал ниже код Java.
String path = "C:\\Temp\\Error_Hunter";
String fileName = "\\nvr-service.txt";
String testWord = "(Error: 87)";
int tLen = testWord.length();
int wordCntr = 0;
String file1 = path + fileName;
boolean check;
try{
FileInputStream fstream = new FileInputStream(file1);
BufferedReader br = new BufferedReader(new InputStreamReader(fstream));
String strLine;
//Read File Line By Line
while((strLine = br.readLine()) != null){
//check to see whether testWord occurs at least once in the line of text
check = strLine.toLowerCase().contains(testWord.toLowerCase());
if(check){
//get the line, and parse its words into a String array
String[] lineWords = strLine.split("\\s+");
for(String w : lineWords)
{
if(w.length() >= tLen){
String word = w.substring(0,tLen).trim();
if(word.equalsIgnoreCase(testWord))
{
wordCntr++;
}
}
}
}
}
System.out.println("total is: " + wordCntr);
//Close the input stream
br.close();
} catch(Exception e){
e.printStackTrace();
}
В моем текстовом файле слово имеет 104 совпадения.но это не найти слово.потому что он содержит пространство между ними.Пожалуйста, предложите что-нибудь или отредактируйте в самом коде.