在我的java代碼中,我創建了執行某些shell命令的場景,實際上具體執行了-scp-
和-mv-
命令。 是否可以記錄執行結果?例如,如果沒有找到要複製的文件,或者-scp-
或-mv-
未正確發生,它就會記錄到我準備好的日誌文件中。目前我的命令執行代碼如下:將命令行參數記錄到log4j中
if ("command") {
String command = "mv " + source_file_path + "/"
+ " " + dest_file_path;
Process child = Runtime.getRuntime().exec(command);
exitVal = child.waitFor();
// Get the input stream and read from it
InputStream in = child.getInputStream();
BufferedInputStream bis = new BufferedInputStream(in);
ByteArrayOutputStream buf = new ByteArrayOutputStream();
int c = bis.read();
while (c != -1) {
byte b = (byte) c;
buf.write(b);
c = bis.read();
}
in.close();
System.out.println("Input Stream: " + buf.toString());
buf.close();
bis.close();
InputStream ein = child.getErrorStream();
BufferedInputStream ebis = new BufferedInputStream(ein);
ByteArrayOutputStream ebuf = new ByteArrayOutputStream();
int ce = ebis.read();
while (ce != -1) {
// process((char)c);
byte be = (byte) ce;
ebuf.write(be);
ce = ebis.read();
}
ein.close();
System.out.println("Error Stream: " + ebuf.toString());
ebuf.close();
ebis.close();
}
System.exit(0);
有無論如何我可以在其上添加一個日誌記錄組件?當文件未找到,文件沒有正確傳輸時,文件傳輸出現問題時記錄。例如
你只是想記錄命令的輸出,或者你想解析命令的輸出並根據情況記錄特定的消息嗎? –
我想根據情況記錄特定的消息。 – user960740