Пустые строки не удаляются регулярным выражением - PullRequest
0 голосов
/ 12 марта 2020

Я читаю в выводе оболочки и отображаю его на консоли. Я изменяю

line

с

String test = line.replaceAll("[\\\r\\\n]+","");

, который, по моему мнению, должен удалить все пустые строки, но вывод не изменяется .

public class tempapp{
    public static void main(String[] args) throws IOException, InterruptedException {
        String fileName = "temps.txt";
        TimerTask task = new TimerTask() {
            @Override
            public void run(){
                // task goes here

                List<String> commands = new ArrayList<>();
                //build command
                commands.add("/usr/bin/sensors");
                //args
                //commands.add("");
                System.out.println(commands);

                ProcessBuilder pb = new ProcessBuilder(commands);
                pb.directory(new File("/home/ethano"));
                pb.redirectErrorStream(true);
                Process process = null;
                try {
                    process = pb.start();
                } catch (IOException e) {
                    e.printStackTrace();
                }

                //Read output
                StringBuilder out = new StringBuilder();
                BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
                String line = null, previous = null;
                while (true) {
                    try {
                        if (!((line = br.readLine()) != null)) break;
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                    if (!line.equals(previous)) {
                        previous = line;
                        out.append(line).append('\n');
                        String test = line.replaceAll("[\\\r\\\n]+","");
                        System.out.println(test);
                    }
                }

                //Check result
                try {
                    if (process.waitFor() == 0) {
                        //System.exit(0);
                    }
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

                //weird termination
                //System.err.println(commands);
                //System.err.println(out.toString());
                //System.exit(1);
            }
        };

        Timer timer = new Timer();
        long delay = 0;
        long intervalPeriod = 4 * 1000;

        //schedules task to run in interval
        timer.scheduleAtFixedRate(task, delay, intervalPeriod);


    }
}
...