2017-07-15 13 views
-1

私は巨大なテキストファイルを持っていますが、特定の単語を検索して3つ以上を印刷したいのですがこれまでにこの数字を印刷しましたテキストファイルで特定の単語を検索するには

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

速い応答のための男ですが、後に3〜4ワードを印刷するにはどうすればよいですか? – 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); 

コンソール出力は次のようになります。

これは、ファイル(プラスこのようなもの)ラインです。

ここにかかわらず、主なものは、あなただけではない、実際の言葉と同じ別のワード内の語句、例えば単語「と」と単語「砂」に対処したいということです。単語と 'を構成する文字は、' sand 'という単語にも含まれていることがわかります。したがって、上記のコード例でも変更されます。 String.contains()メソッドも、この方法で文字列を検索します。ほとんどの場合、単語全体のみを扱う場合は、String.replaceAll()という方法でRegular Expression(RegEx)を使用するのが簡単な解決策である場合は、これは望ましくありません。

:あなたはもちろん、具体的ラインで String.replaceAll()方法で使用する正規表現内単語境界メタ文字.B \エスケープ に気づくでしょう

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

:独自のコードを使用して、それは次のようになります

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

これにより、単語全体を処理することができます。

関連する問題