使用java在文本文件中搜索特定单词

时间:2017-07-15 19:31:55

标签: java

我有一个巨大的文字文件,我想搜索特定的字词并打印三个或更多,然后打印这个字数到目前为止我已经完成了这个

public static void main(String[] args) {
    String fileName = "C:\\Users\\Mishari\\Desktop\\Mesh.txt";        
    String line = null;
    try {            
        FileReader fileReader = 
            new FileReader(fileName);

        BufferedReader bufferedReader = 
            new BufferedReader(fileReader);

        while((line = bufferedReader.readLine()) != null) {                
            System.out.println(line);
        }   

        bufferedReader.close();         
    } catch(FileNotFoundException ex) {
        System.out.println(
            "Unable to open file '" + 
            fileName + "'");                
    } catch(IOException ex) {
        System.out.println(
            "Error reading file '" 
            + fileName + "'");                  
    }  
}

仅用于打印文件,您可以告诉我最好的方法是什么。

3 个答案:

答案 0 :(得分:3)

您可以使用此方法查找单词索引。

int index = line.indexOf(word);
  • 如果索引为-1,则该单词不存在。
  • 如果它存在而不是从该索引开始直到行尾的行的子字符串。

    String nextWords = line.substring(index);
    
  • 现在使用String[] temp = nextWords.split(" ")获取该子字符串中的所有字词。

答案 1 :(得分:0)

    while((line = bufferedReader.readLine()) != null) {
        System.out.println(line);
        if (line.contains("YOUR_SPECIFIC_WORDS")) { //do what you need here }
    }   

答案 2 :(得分:0)

通过它的声音你似乎正在寻找的是一个基本的Find&替换从文件读入的每个文件行的所有机制。换句话说,如果当前读取的文件行恰好包含 Word 或短语,那么您希望添加单词,然后用相同的单词替换找到的单词加上你想要添加的其他词。从某种意义上说,它会是这样的:

String line = "This is a file line.";
String find = "file";  // word to find in line
String replaceWith = "file (plus this stuff)"; // the phrase to change the found word to.
line = line.replace(find, replaceWith);  // Replace any found words
System.out.println(line);

控制台输出将是:

  

这是一个文件(加上这个东西)行。

这里最重要的是你只想处理实际的单词而不是另一个单词中的相同短语,例如单词&#34;和&#34; 和单词<强>&#34;沙&#34; 即可。您可以清楚地看到构成单词&#39;和的字符也位于单词&#39; sand&#39; 中,因此它也会被上面的示例代码更改。 String.contains()方法也以这种方式定位字符串。在大多数情况下,如果您只想专门处理整个单词,这是不可取的,因此一个简单的解决方案是使用Regular Expression(RegEx)和 String.replaceAll() 方法。使用您自己的代码,它看起来像这样:

String fileName = "C:\\Users\\Mishari\\Desktop\\Mesh.txt";
String findPhrase = "and"; //Word or phrase to find and replace
String replaceWith = findPhrase + " (adding this)";  // The text used for the replacement.
boolean ignoreLetterCase = false; // Change to true to ignore letter case
String line = "";

try {
    FileReader fileReader = new FileReader(fileName);
    BufferedReader bufferedReader = new BufferedReader(fileReader);

    while ((line = bufferedReader.readLine()) != null) {
        if (ignoreLetterCase) {
            line = line.toLowerCase();
            findPhrase = findPhrase.toLowerCase();
        }
        if (line.contains(findPhrase)) {
            line = line.replaceAll("\\b(" + findPhrase + ")\\b", replaceWith);
        }
        System.out.println(line);
    }
    bufferedReader.close();
} catch (FileNotFoundException ex) {
    System.out.println("Unable to open file: '" + fileName + "'");
} catch (IOException ex) {
    System.out.println("Error reading file: '" + fileName + "'");
}

您当然会注意到 String.replaceAll() 方法中使用的正则表达式中的转义 \ b 字边界元字符,具体如下:

line = line.replaceAll("\\b(" + findPhrase + ")\\b", replaceWith);

这使我们只能处理整个单词。

相关问题