write the content of the mac os terminal to a file in java
I am new to mac os. I have used the following code in java to get the s开发者_开发技巧ystem details. It opens a terminal to display the result of that command. I need to get those details in a file. How can I get it.
String[] command = {"/usr/bin/open", "/Applications/Utilities/System Profiler.app"};
Process proc = Runtime.getRuntime().exec(command);
int resultCode = proc.waitFor();
if (resultCode != 0) {
throw new Exception("failed to open system profiler");
}
By opening an application to the user, you can not deal with any results. You need to use the commandline-version of the "System Profiler", which prints it's results back to the shell. You can then deal with the result in Java, by using the following code:
String[] command = {"/usr/sbin/system_profiler"}
String sendback = "";
Process proc = Runtime.getRuntime().exec(command);
InputStream istr = proc.getInputStream();
BufferedReader br = new BufferedReader(new InputStreamReader(istr));
String str;
while ((str = br.readLine()) != null) {
sendback = sendback + str;
}
int resultCode = proc.waitFor();
br.close();
if (resultCode != 0) {
throw new Exception("failed to open system profiler");
}
Within Java, you can deal with the result in any way (write it to a file or whatever).
But if you really want to simply write it into a file (and not use it in Java), you can do it, by changing your command and redirecting the output into the target-file on your shell:
/usr/sbin/system_profiler >/path/to/yourfile.txt
精彩评论