在文本文件中搜索多行字符串

珍妮:

我有我正努力寻找具有多行字符串的文本文件。一个单一的字符串,我能够搜索,但我需要多行被搜索的字符串。

我试图寻找这是工作的罚款单行。

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

现在,检查searchStringlineInFilecontains

StringBuilder searchString = new StringBuilder();

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

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

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章

在文本文件中搜索多行字符串,然后在Python中返回行号

在matlab中搜索和替换文本文件的多行中的特定字符串

在文本文件中搜索字符串?

在文本文件中搜索字符串的位置

如何搜索字符串并替换文本文件中的字符串?

搜索文本文件,直到特定的字符串

如何使用python在文本文件中写入多行字符串的完整输出?

如何提取文本文件中的特定行以及包含特定字符串的多行?

Python:文本文件在多行中替换不同的字符串怎么办?

PHP在文本文件中搜索字符串并在特定字符后返回结果

在文本文件中搜索多个字符串,如果退出则删除文件

在多个文件/目录中搜索字符串,然后打印文本文件的内容

在R中的文本文件中搜索字符串

在 Powershell 中的文本文件中搜索多个字符串

资源下载搜索文本文件和字符串或替换字符串中删除字符

在大型文本文件中搜索字符串-在python中分析各种方法

从文本文件Ubuntu中搜索和替换字符串

从文本文件中搜索字符串并显示选定的行

Python:在文本文件中搜索字符串后查找值

如何在文本文件中搜索字符串?

如何在文本文件中搜索多个字符串

在多个文本文件中搜索两个字符串?

Bash脚本在文本文件中搜索多个字符串

在文本文件中搜索字符串时如何包含空格

在文本文件中搜索具有今天日期的字符串

如何使用Qt在文本文件中搜索字符串

在文本文件中搜索关键字,直到遇到字符串

搜索并拆分文本文件中的某些字符串并保存输出

在文本文件中搜索字符串并在PHP中出现空格后打印