2017-07-15 17 views
-1

在文本文件中的特定詞搜索我一個巨大的文本文件,我想搜索特定的詞和打印三個或更多的則這個數字的字樣後,到目前爲止,我已經這樣做了使用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 + "'");     
    } 
} 

它只是打印文件,你可以告訴我什麼是做這件事的最好方法。

回答

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

thanx男子爲快速反應,但我怎麼能打印後三個或四個單詞? – Mesh

3

您可以使用此方法查找字符索引。

int index = line.indexOf(word); 
  • 如果該指數爲-1,那麼這個詞不存在。
  • 如果它存在比從該索引開始直到行尾的行的子字符串。

    String nextWords = line.substring(index); 
    
  • 現在使用String[] temp = nextWords.split(" ")來獲取該子字符串中的所有單詞。

0

通過它你看起來是在尋找的聲音是一個基本的查找&全部替換機制,是從文件中讀取每個文件一行。換句話說,如果當前正在讀取的文件行碰巧包含文字或短語,您希望添加單詞,然後用相同的單詞加上您想要添加的其他單詞來替換該單詞。在某種意義上,它會是這樣的:

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); 

控制檯輸出將是:

這是一個文件(加上這東西)線。

這裏雖然主要的事情是,你只需要應付實際的話,而不是另一個詞在同一短語,例如字「和」和單詞「沙」。您可以清楚地看到,組成單詞'和'的字符也位於單詞'沙'中,因此它也將隨上述示例代碼而更改。 String.contains()方法也以這種方式查找字符串。在大多數情況下,如果您只想專門處理整個單詞,那麼這是不受歡迎的,所以一個簡單的解決方案就是使用Regular Expression(RegEx)和方法。使用自己的代碼,它會是這個樣子:

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 + "'"); 
} 

你當然會注意到逃脫\ b字特別是在線路的String.replaceAll()方法所使用的正則表達式中邊界元字符:

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

這使我們只能處理整個單詞。