【发布时间】:2019-10-24 00:03:44
【问题描述】:
我有一个文本文件,我试图从中搜索一个包含多行的字符串。我可以搜索单个字符串,但我需要搜索多行字符串。
我试图搜索运行良好的单行。
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
【问题讨论】:
-
如果两条线不相邻怎么办?
-
那么它不需要搜索。只有当行相邻时才应该搜索。
-
line one line two line three line four这些是您从文件中读取的搜索值,还是您在不同位置有搜索值并在文件内搜索?
标签: java string file bufferedreader multiline