Java - 删除行之间的文本

时间:2013-04-27 00:50:49

标签: java string line

我正在尝试删除两个行号之间的文本。我在Ubuntu中使用dmesg,这是一个非常长的命令,所以我想修改第0行之间的字符串,但是留下4到5,等等。这是我的代码:

            try {

                Process terminal = Runtime.getRuntime().exec("dmesg");
                BufferedReader in = new BufferedReader(new InputStreamReader(terminal.getInputStream()));
                String line;
                String output = "";

                while((line = in.readLine()) != null) {

                   output += line + "\n";

                }

                System.out.println(output);
                in.close();

            } catch(Exception exc) {

                System.err.println("An error occurred while executing a command in the terminal. Error:\n" + exc);

            }

有什么建议吗?谢谢!

1 个答案:

答案 0 :(得分:1)

试试这个:

try {

        Process terminal = Runtime.getRuntime().exec("dmesg");
        BufferedReader in = new BufferedReader(new InputStreamReader(terminal.getInputStream()));
        String line;
        String output = "";
        int counter = 0;
        while((line = in.readLine()) != null) {
            if (counter > 3) output += line + "\n";
            counter++;
        }

        System.out.println(output);
        in.close();

    } catch(Exception exc) {

        System.err.println("An error occurred while executing a command in the terminal. Error:\n" + exc);
    }