JAVA 执行windows系统命令指令

public class OSExecuteString {

    /**
* 执行命令返回信息
*/
private  String sMessage = "";

/**
* 执行命令返回错误信息
*/
private  String sError = "";

    /**
     * 分隔符
     */
    private String sLine = "\n";


    /**
*
*/
public OSExecuteString() {
// TODO Auto-generated constructor stub
}

/**
*
* 类名: OSExecuteString
* 方法名: commandString
* 返回: void
* 参数: @param command  系统命令指令
*/
public  void commandString(String command) {
StringBuffer message = new StringBuffer(100000);
StringBuffer error = new StringBuffer(100000);
try {
Process process = new ProcessBuilder(command.split(" ")).start();
BufferedReader results = new BufferedReader(new InputStreamReader(
process.getInputStream()));
String s;
while ((s = results.readLine()) != null){
                          message.append(s+sLine);

}
BufferedReader errors = new BufferedReader(new InputStreamReader(
process.getErrorStream()));
while ((s = errors.readLine()) != null) {

                    error.append(s+sLine);
}
} catch (Exception e) {
if (!command.startsWith("CMD /C")){
commandString("CMD /C " + command);
}else{
throw new RuntimeException(e);
}
}finally{
sMessage = message.toString();
sError = error.toString();
}

}

/**
* @return the sMessage
*/
public String getSMessage() {
return sMessage;
}

/**
* @return the sError
*/
public String getSError() {
return sError;
}

    public String getSLine() {
        return sLine;
    }

    public void setSLine(String sLine) {
        this.sLine = sLine;
    }
}

你可能感兴趣的:(java,C++,c,windows,C#)