当前位置:首页 > 开发 > 编程语言 > Java > 正文

java 进程调用

发表于: 2014-05-10   作者:antlove   来源:转载   浏览:
摘要:   package process; import java.io.BufferedReader; import java.io.InputStream; import java.io.InputStreamReader; public class ProcessExecutor { public static void execute(String command

 

package process;

import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;

public class ProcessExecutor {
	public static void execute(String command)throws Exception{
		Process process = Runtime.getRuntime().exec(command);
		// the exit value
		int exitValue = process.waitFor();
		System.out.println("the exit value is "+exitValue);
		
		String line = null;
		
		// the output message from sub process
		InputStream inputStream = process.getInputStream();
		BufferedReader inputStreamReader = new BufferedReader(new InputStreamReader(inputStream));
		StringBuilder sbForInputStream = new StringBuilder();
        while ((line = inputStreamReader.readLine()) != null) { 
        	sbForInputStream.append(line); 
        } 
        inputStreamReader.close();
        System.out.println("below is the output message :\n"+sbForInputStream);
        
    	// the output error message from sub process
        InputStream errInputStream = process.getErrorStream();
		BufferedReader errInputStreamReader = new BufferedReader(new InputStreamReader(errInputStream));
		StringBuilder sbForErrInputStream = new StringBuilder();
	    line = null; 
        while ((line = errInputStreamReader.readLine()) != null) { 
        	sbForErrInputStream.append(line); 
        } 
        errInputStreamReader.close();
        System.out.println("below is the error output message :\n"+sbForErrInputStream);
	}
	
	public static void main(String[] args)throws Exception {
//		execute("cmd /c dir c: ");
		execute("cmd /c del C:\\Users\\dell-pc\\Desktop\\test\\e.txt");
	}
}

 

java 进程调用

  • 0

    开心

    开心

  • 0

    板砖

    板砖

  • 0

    感动

    感动

  • 0

    有用

    有用

  • 0

    疑问

    疑问

  • 0

    难过

    难过

  • 0

    无聊

    无聊

  • 0

    震惊

    震惊

版权所有 IT知识库 CopyRight © 2009-2015 IT知识库 IT610.com , All Rights Reserved. 京ICP备09083238号