テキストファイルに複数行の文字列を検索

Janny:

私は私が複数の行を持っている文字列を検索しようとしていたから、テキストファイルを持っています。私が検索できるようにしていますが、私は複数行の文字列を必要とする単一の文字列を検索します。

私は正常に動作している単一の行を検索しようとしています。

public static void main(String[] args) throws IOException 
{
  File f1=new File("D:\\Test\\test.txt"); 
  String[] words=null;  
  FileReader fr = new FileReader(f1);  
  BufferedReader br = new BufferedReader(fr); 
  String s;     
  String input="line one"; 

  // here i want to search for multilines as single string like 
  //   String input ="line one"+
  //                 "line two";

  int count=0;   
  while((s=br.readLine())!=null)   
  {
    words=s.split("\n");  
    for (String word : words) 
    {
      if (word.equals(input))   
      {
        count++;    
      }
    }
  }

  if(count!=0) 
  {
    System.out.println("The given String "+input+ " is present for "+count+ " times ");
  }
  else
  {
    System.out.println("The given word is not present in the file");
  }
  fr.close();
}

そして、以下のファイルの内容があります。

line one  
line two  
line three  
line four
デッドプール :

使用しStringBuilderたファイルからすべての行を読み込み、それらを追加し、そのためStringBuilderlineSeparator

StringBuilder lineInFile = new StringBuilder();

while((s=br.readLine()) != null){
  lineInFile.append(s).append(System.lineSeparator());
}

今すぐチェックsearchStringしてlineInFile使用することにより、contains

StringBuilder searchString = new StringBuilder();

builder1.append("line one");
builder1.append(System.lineSeparator());
builder1.append("line two");

System.out.println(lineInFile.toString().contains(searchString));

おすすめ

転載: http://43.154.161.224:23101/article/api/json?id=188527&siteId=1