Parse cursor output java

旧城冷巷雨未停 提交于 2019-12-08 06:24:08

问题


How can i parse the output from a linux cursor program like e.g top in java? I would like someone to give an example or link one. Right now i got top running like a Process object. And btw top is just an example of such a program.

String[] args={"top"};
Process process = new ProcessBuilder(args).start();

回答1:


You can't. A program written using curses isn't outputting a stream of characters like a typical command-line program, or even one using the backspace trick. Instead, it's using operating-system specific calls like ioctl and implementation-specific escape sequences like those described in TermInfo. Generally, such programs on Unix systems are simple frontends to libraries that perform all of the necessary work, and your best option is usually to use those libraries (writing a JNI wrapper if one isn't available).

In the case of top, you can see how the program reads the process information from the OS in its source code, available as part of the procps package.




回答2:


The first step would be to read all the data that process outputs, which can be done using Process.getInputStream() method

Process p = new ProcessBuilder("top").start();
BufferedReader br  =  new BufferedReader(new InputStreamReader(p.getInputStream()));
String line;
while((line = br.readLine()) != null) {
    System.out.println(line);
}


来源:https://stackoverflow.com/questions/17975540/parse-cursor-output-java

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!