import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 控制檯處理工具箱
*
* @author leizhimin 2009-6-25 14:12:14
*/
public
final
class CmdToolkit {
private
static Log log = LogFactory.getLog(CmdToolkit.
class);
private CmdToolkit() {
}
/**
* 讀取控制命令的輸出結果
*
* @param cmd 命令
* @param isPrettify 返回的結果是否進行美化(換行),美化意味着換行,默認不進行美化,當此參數爲null時也不美化,
* @return 控制命令的輸出結果
* @throws IOException
*/
public
static String readConsole(String cmd, Boolean isPrettify)
throws IOException {
StringBuffer cmdout =
new StringBuffer();
log.info(
"執行命令:" + cmd);
Process process = Runtime.getRuntime().exec(cmd);
//執行一個系統命令
InputStream fis = process.getInputStream();
BufferedReader br =
new BufferedReader(
new InputStreamReader(fis));
String line =
null;
if (isPrettify ==
null || isPrettify) {
while ((line = br.readLine()) !=
null) {
cmdout.append(line);
}
}
else {
while ((line = br.readLine()) !=
null) {
cmdout.append(line).append(System.getProperty(
"line.separator"));
}
}
log.info(
"執行系統命令後的結果爲:\n" + cmdout.toString());
return cmdout.toString().trim(); } }