从一个文件写入另一个条件的日志行

时间:2018-02-05 19:03:52

标签: java

我在将行从一个文件写入另一个文件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();
        }
    }
}

如何写入包含'我感兴趣的标签'的所有内容?

0 个答案:

没有答案