我在将行从一个文件写入另一个文件specyfic行时遇到问题 我的日志。
我的日志文件有结构:
some data
some data
<tag which I am interested in>
important data
</tag which I am interested in>
some data
some data
我的代码:
import java.io.*;
public class Main {
public static void main(String[] args) {
String filePath = "myImputLog.log";
readWriteFile(filePath);
}
public static void readWriteFile(String filePath){
try {
FileReader fileReader = new FileReader(filePath);
BufferedReader bufferedReader = new BufferedReader(fileReader);
FileWriter fileWriter =new FileWriter("myOutPut.txt");
String line;
while ((line = bufferedReader.readLine())!=null){
System.out.println(line);
if ((line.matches("(?i).*<tag which I am interested in>.*"))){
fileWriter.write(line);
fileWriter.write(System.lineSeparator());
}
if ((line.matches("(?i).*</tag which I am interested in>.*"))){
fileWriter.write(line);
fileWriter.write(System.lineSeparator());
}
}
fileReader.close();
fileWriter.close();
}
catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
如何写入包含'我感兴趣的标签'的所有内容?