简体   繁体   English

大家好,我想使用Java代码在我的文本文件中搜索单词“((错误:87)”)

[英]Hi All, I want to search this word “(Error: 87)” in my text file using Java code

I was trying to search this complete word "(Error: 87)" in my text file. 我试图在我的文本文件中搜索完整的单词“(错误:87)”。 I used below java code. 我用下面的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();
    }

In my text file, the word has 104 hits. 在我的文本文件中,该单词具有104个匹配项。 but this is not finding the word. 但这找不到单词。 because it contain space in between. 因为它之间包含空格。 Kindly suggest something or edit in the code itself. 请提出一些建议或在代码本身中进行编辑。

Instead of 代替

String[] lineWords = strLine.split("\\s+");

do

String[] tokens = strLine.split("(Error: 87)");

And then the number of occurrences of (Error: 87) in that line would be tokens.length - 1 . 然后,该行中(Error: 87)的出现次数将是tokens.length - 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我想更改文件中的文本,我的代码正在搜索单词,但不替换单词 - i want to change the text in a file, my code is searching the word but not replacing the word 如何使用Java搜索用户确定的单词并计算文本文件中的出现次数? - How would I search for a user determined word and count the occurrences in a text file using java? 在Ubuntu 16.04中使用JAVA中的MapReduce在文本文件中搜索给定单词 - search a given word in a text File using MapReduce in JAVA in ubuntu 16.04 如何在Java中的文本文件中搜索特定单词 - how to search for a certain word in a text file in java 我的excel文件或文本文件中有一百万条记录,我想使用Java中的多线程概念将该数据插入数据库 - I have a one million records in my excel file or text file, i want to insert that data to database using multi threading concept in java 如何使用扫描仪替换文本文件中的单词? - How do I replace a word in my text file using Scanner? 我通过使用Java代码通过eclipse生成了开放单词文件 - I coudent open word file generated by eclipse using a java code 我想使用itext java将图像拖到我的pdf文件中 - I want to drag an image to my pdf file using itext java 如何使用Java搜索文件中的单词 - How to search for a word in a file using java 我想在文件中搜索Java中的字符串 - I want to Search a file for a String in Java
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM