java方法执行控制台命令工具类

package com.yanek.util;

import java.io.IOException;
import java.io.InputStreamReader;
import java.io.LineNumberReader;

public class Tool {

	/**
	 * @param args
	 */
	public static void main(String[] args) {

		System.out.println(executeCommond("ipconfig"));

	}
     /**
      *  java方法执行控制台命令,返回执行的输入结果
      * @param cmd
      * @return
      */
	public static String executeCommond(String cmd) {
		String ret = "";
		try {
			Process p = Runtime.getRuntime().exec(cmd);
			InputStreamReader ins = new InputStreamReader(p.getInputStream());
			LineNumberReader input = new LineNumberReader(ins);

			String line;
			while ((line = input.readLine()) != null) {
				System.out.println(line);
				ret += line + "
"; } } catch (IOException e) { e.printStackTrace(); } return ret; } }


 

你可能感兴趣的:(Java技术)