java 執行命令行並打印返回結果

package test;

import java.io.ByteArrayOutputStream;
import java.io.InputStream;
/**
 * @author 作者 路飛: 
 * @version 創建時間:2013-6-18 上午11:44:03
 * 類說明 執行命令行並打印返回結果
 */
public class Demo{   
     public static void main(String args[]){   
     Runtime rn = Runtime.getRuntime();   
     Process p = null;   
     String cmdStr = "";   
     try{   
    	 cmdStr = "arp -a";   
       p = rn.exec(cmdStr);  
       
      //結果自己解析
      System.out.println( new String(read(p.getInputStream())));
     }catch( Exception e ){   
        e.printStackTrace();   
     }   
    }   
 	public static byte[] read(InputStream inStream) throws Exception{
		ByteArrayOutputStream outputStream = new ByteArrayOutputStream();
		byte[] buffer = new byte[1024];
		int len = 0;
		while( (len = inStream.read(buffer)) != -1){
			outputStream.write(buffer, 0, len);
		}
		inStream.close();
		return outputStream.toByteArray();
	}
}  

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章