2016-06-29 6 views
-1

ディレクトリ内のすべてのファイル内の特定の文字列を検索します。ディレクトリ内のすべてのファイルで文字列を検索します。

例:/テスト/章/私が試してみました

D:/test/chapters 
      /chapter1.log 
      /chapter2.log 
      /chapter3.log all these sub files under D:/test/chapters/ . 

サンプルコード:

public class Example { 
    public Example() { 
     super(); 
    } 

    public int plugin_execute() { 
     boolean foundstring=false; 
     try { 
      File dir = new File("D:/test/chapters"); 
      String[] children = dir.list(); 
      if (children == null) { 
       System.out.println("does not exist is not a directory"); 
      } else { 
       for (int i = 0; i < children.length; i++) { 
        String filename = children[i]; 
        System.out.println(filename); 

        if (filename !=null) { 
         foundstring = testString(filename, "tiger"); 
         System.out.println("failed"); 
        }  

        //Search for entry in file 

        if (!foundstring) { 
         return //failuremsg 
        } else { 
         System.out.println("failed"); 
         return //succes 
        } 
       } 
      } 
      return 1; 
     } catch (Exception e) { 
      return //error mssg 
     } 
    } 

    private boolean teststring(String filePath, String str) { 
     BufferedReader br = null; 
     File file = new File(filePath); 
     boolean result = false; 
     if(!file.exists()) 
      return false; 
     try { 
      br = new BufferedReader(new FileReader(filePath)); 
      String sCurrentLine; 
      while ((sCurrentLine = br.readLine()) != null) { 
       if (sCurrentLine.contains(str)) { 
        result = true; 
        System.out.println(str); 
        System.out.println("Found entry "); 
        break; 
       } 
      } 
     } catch (IOException e) { 
      e.printStackTrace(); 
     } finally { 
      try { 
       if (br != null)br.close(); 
      } catch (IOException ex) { 
       ex.printStackTrace(); 
      } 
     } 
     return result; 
    } 
} 

それが唯一の最後のファイルの出力を返し、手段パスDの "虎" を検索します最後のファイルで成功を検索すると、それ以外の場合は成功します。 しかし、文字列が最初のファイルに見つかったら成功したい。つまり、第1章が...どのように私はこのコードを変更することができます示唆してください

....第1章では見つからなかった場合、それは第2章では、検索を続けるべき、成功を返す必要があり

+0

チェックhttp://stackoverflow.com/questions/1844688/read-all-files-in-a-folder –

+0

サイドノート - あなたは、Java 7を使用している場合は、[試す-で-リソース]を使用(https://docs.oracle.com/javase/tutorial/essential/exceptions/tryResourceClose.html) – 4castle

+0

ディレクトリ内の文字列を検索したい – user092

答えて

0

問題:!シンプルなミックスアップとtrue/falseロケーション。

ソリューション:私はあなたのコード内で参照信じる変更この

if (! foundString) 
{ 
    return // failuremsg 
} 
else 
{ 
    System.out.println("failed"); 
    return // success 
} 

if (foundString) 
{ 
    // return success message 

} 
else 
{ 
    // return failure message 
} 

へのもう一つの問題は、他の一方で、という方法 findStringを呼び出す foundstring = findString(filename, "tiger");ラインでありますあなたのコードに投稿したメソッドは testStringです。私はこれが名前のミックスであると仮定します。

+0

ミックスアップ関数名について同意します...私はそれを逃しましたが、if(if found!)if私の問題は解決しますか? – user092

+0

@ user092 'if'文の先頭に到達した時点で、検索文字列がファイル内に見つかった場合、変数' foundString'はtrueに設定されています。あなたが投稿したコードで 'if(!foundString)'、 '!'は 'foundString'の値を無効にし、あなたが望むものと反対のものに変更します。 – Queue

+0

"Tiger"を含む最初のファイルでこのコードを実行したところ、 'return true'が返され、アプリケーションが終了しました。 – Queue

0
public void listFiles(Path dir , String text) 
{ 
    try (DirectoryStream<Path> directoryStream = Files.newDirectoryStream(dir)) 
    { 
     for (Path path : directoryStream) 
     { 
      if (Files.isRegularFile(path) && Files.isReadable(path)) 
      { 
       //this.findString(path, text); 
      } 
     } 
    } 
    catch (IOException ex) 
    { 
     ex.printStackTrace(); 
    } 
} 

private boolean findString(Path file, String text) 
{ 
    //Your implementation 
    return true; 
} 
関連する問題