如何删除Java文件中以特定字符串开头和结尾的特定行?

时间:2019-05-15 17:43:45

标签: java

我有一个文件abc.txt,其行为

abc.txt

Ethernet 1/1 

Ethernet 1/2

interface 3

abs_mod_
jjj
kkkk
ll
abs_mod_

interface 6

interface 7

我想删除abs_mod_之间的行以及abs_mod_开头的行

最终输出应为

abc.txt

Ethernet 1/1

Ethernet 1/2

interface 3

interface 6

interface 7

我尝试过逐行读取文件。

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
    import java.io.IOException;

public class ReadStringFromFileLineByLine {

    public static void main(String[] args) {
        try {
            File file = new File("abc.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            while ((line = bufferedReader.readLine()) != null) {
                stringBuffer.append(line);
                stringBuffer.append("\n");
            }
            fileReader.close();
            System.out.println("Contents of file:");
            System.out.println(stringBuffer.toString());
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

2 个答案:

答案 0 :(得分:1)

幼稚的方法:

import sys
from PyQt5 import QtWidgets

if __name__ == '__main__':

    app = QtWidgets.QApplication(sys.argv)

    window = QtWidgets.QWidget()
    window.setGeometry(0, 0, 500, 300)
    window.setWindowTitle("UI")

    btn = QtWidgets.QPushButton("Test")

    lay = QtWidgets.QHBoxLayout(window)
    lay.addWidget(btn)
    window.show()

    sys.exit(app.exec_())

使用正则表达式:

package test;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;

public class ReadStringFromFileLineByLine {

    public static void main(String[] args) {
        try {
            File file = new File("abc.txt");
            FileReader fileReader = new FileReader(file);
            BufferedReader bufferedReader = new BufferedReader(fileReader);
            StringBuffer stringBuffer = new StringBuffer();
            String line;
            boolean flag = true;
            while ((line = bufferedReader.readLine()) != null) {
                if(line.contains("abs_mod_")){
                    if(flag)    flag = false;
                    else        flag = true;
                }else if(flag){
                    stringBuffer.append(line);
                    stringBuffer.append("\n");
                }
            }
            fileReader.close();
            System.out.println("Contents of file:");
            System.out.println(stringBuffer.toString().replace("\n\n\n", "\n\n"));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

答案 1 :(得分:0)

将整个文件加载到内存中,然后使用正则表达式删除不需要的行。

使用Java 11 +

String text = Files.readString(Paths.get("abc.txt"));
text = text.replaceAll("(?sm)^abs_mod_(?:.*?^abs_mod_)?.*?\\R", "");
System.out.println(text);

使用Java 7 +

String text = new String(Files.readAllBytes(Paths.get("abc.txt")), StandardCharsets.UTF_8);
text = text.replaceAll("(?sm)^abs_mod_(?:.*?^abs_mod_)?.*?\\R", "");
System.out.println(text);

输出

Ethernet 1/1 

Ethernet 1/2

interface 3


interface 6

interface 7

说明

(?              Set flags:
  s               DOTALL     '.' matches any character, including a line terminator
  m               MULTILINE  '^' and '$' match just after/before a line terminator
)
^abs_mod_       Match 'abs_mod_' at beginning of line
(?:             Start optional non-capturing group
  .*?             Match any text (including line terminators) until:
  ^abs_mod_         Match 'abs_mod_' at beginning of line
)?              End optional section
.*?             Match any text up to:
\R                Match line terminator

两个.*都有额外的?,这使它们变得“不愿意”,因此它们不会越过“结束”比赛。第二个.中的.*?实际上不会与行终止符匹配,因为结尾匹配 是行终止符。

可选部分是因为您说过:“删除abs_mod_之间的行,以及以 abs_mod_开头的行”

正则表达式实际上是将这两者融合在一起:

(?sm)^abs_mod_.*?^abs_mod_.*?\R   Lines between lines starting with 'abs_mod_' (inclusive)
(?m:^)abs_mod_.*\R                Single line starting with 'abs_mod_'