删除包含特定字符的行

时间:2014-03-24 18:15:14

标签: java

我有一个像这样的文本文件,这只是一个示例:

# Timestamp, X, Y, MAC Address of AP, RSS
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:01 -77
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:02 -77
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:a2 -72
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:c2 -69
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:d1 -86
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:d2 -87
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:12 -96
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:f1 -65
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:f2 -67
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:e2 -61
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:84:92:12 -95
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:85:a4:00 -77
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:04:d0 -87
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:87:03:f0 -70
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:b1 -90
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:b2 -92
1395422922871 6.241992473602295 0.9473437666893005 00:1a:1e:8e:e9:b0 -91

我想编写一个搜索以下字符的Java程序:00:1a:1e:85:a4:00和00:1a:1e:87:04:d0并删除包含这些字符的行。

我有以下程序:

public class extract {
    public static void main (String[] args) throws java.lang.Exception
    {
    File inputFile = new File("myFile.txt");
    File tempFile = new File("myTempFile.txt");

    BufferedReader reader = new BufferedReader(new FileReader(inputFile));
    BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));

    String lineToRemove = "00:1a:1e:87:04:d0";
    String currentLine;

    while((currentLine = reader.readLine()) != null) {
        // trim newline when comparing with lineToRemove
        String trimmedLine = currentLine.trim();
        if(trimmedLine.equals(lineToRemove)) continue;
        writer.write(currentLine);
    }

    writer.close();
    boolean successful = tempFile.renameTo(inputFile);
    }

}

但它只能删除与整行匹配的字符,输出文件只包含在一行中,您对我需要修改哪些内容以获得所需的输出有什么建议吗?

1 个答案:

答案 0 :(得分:5)

这是我认为应该是这样的。我可能在try()位上有格式错误,但看看是否编译。

Java 1.6:

public class Extract {
    public static void main (String[] args) throws java.lang.Exception {

        final File inputFile = new File("myFile.txt");
        final File tempFile = new File("myTempFile.txt");

        BufferedReader reader = null;
        BufferedWriter writer = null;
        try {
            reader = new BufferedReader(new FileReader(inputFile));
            writer = new BufferedWriter(new FileWriter(tempFile));

            final String lineToRemove1 = "00:1a:1e:87:04:d0";
            final String lineToRemove2 = "00:1a:1e:85:a4:00";
            String currentLine;

            while((currentLine = reader.readLine()) != null) {
                // trim newline when comparing with lineToRemove
                final String trimmedLine = currentLine.trim();
                if (!currentLine.contains(lineToRemove1) 
                        && !currentLine.contains(lineToRemove2)) {
                    writer.write(currentLine);
                    writer.newLine();
                }
            }
        } finally {
            if (reader != null) { 
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }
}

Java 1.7+:

public class Extract {
    public static void main (String[] args) throws java.lang.Exception {

        final File inputFile = new File("myFile.txt");
        final File tempFile = new File("myTempFile.txt");

        try(
            final BufferedReader reader = new BufferedReader(new FileReader(inputFile));
            final BufferedWriter writer = new BufferedWriter(new FileWriter(tempFile));
        ) {

            final String lineToRemove1 = "00:1a:1e:87:04:d0";
            final String lineToRemove2 = "00:1a:1e:85:a4:00";
            String currentLine;

            while((currentLine = reader.readLine()) != null) {
                // trim newline when comparing with lineToRemove
                final String trimmedLine = currentLine.trim();
                if (!currentLine.contains(lineToRemove1) 
                        && !currentLine.contains(lineToRemove2)) {
                    writer.write(currentLine);
                    writer.newLine();
                }
            }
        } finally {
            if (reader != null) { 
                reader.close();
            }
            if (writer != null) {
                writer.close();
            }
        }
    }
}