我有一个文本文件,想要查找其中包含多行的字符串。我可以查找单行字符串,但需要查找多行字符串。
我尝试过搜索单行字符串,结果正常。
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
第二行
第三行
第四行
这些是您从文件中读取的搜索值,还是您在不同的地方拥有搜索值并在文件内进行搜索? - Tenusha Guruge