java 实现windows 命令行

package testproject;

import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;

public class TestProcess {
	
	static SystemIn input;
	static SystemOut output;
	static SystemErr errput;
	static Process process;
	public static void main(String[] args) throws Exception{
		ProcessBuilder processBuilder=new ProcessBuilder("cmd");
		
		 process=processBuilder.start();
		input=new SystemIn(process.getInputStream());
		output=new SystemOut(process.getOutputStream());
		errput=new SystemErr(process.getErrorStream());
		input.start();
		output.start();
		errput.start();
		 int i=process.waitFor();
		 System.out.println(i);
		 //TODO 通过NIO来实现子线程的关闭
		System.exit(0);
	}
	static int stopProcess() {
		System.out.println(process.exitValue());
		return process.exitValue();

	}
static class SystemIn extends Thread{
	boolean flag=true;
 		InputStream in;
 		
		public SystemIn(InputStream in) {
			super();
			this.in = in;
		}

		public InputStream getIn() {
			return in;
		}

		public void setIn(InputStream in) {
			this.in = in;
		}

		@Override
		public void run() {
			InputStreamReader reader = null;
			try {
				reader = new InputStreamReader(in,"GBK");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			int i;
			while(flag){
				try {
				if((i=reader.read())!=-1)
					System.out.print((char)i);
			
				} catch (IOException e) {
					e.printStackTrace();
					
					break;
				}
			}
		}
 		
 	}static class SystemOut extends Thread{
 		boolean flag=true;
 		OutputStream out;
 		
		public SystemOut(OutputStream out) {
			super();
			this.out = out;
		}

		public OutputStream getOut() {
			return out;
		}

		public void setOut(OutputStream out) {
			this.out = out;
		}

		@Override
		public void run() {
			while(flag){
				try {
					char c=(char) System.in.read();
					out.write(c);
					if(c==10)
						out.flush();
				} catch (IOException e) {e.printStackTrace();
					break;
				}
			}
		}
 		
 	}static class SystemErr extends Thread{
 		InputStream err;
 		
		public SystemErr(InputStream err) {
			super();
			this.err = err;
		}

		public InputStream getErr() {
			return err;
		}

		public void setErr(InputStream err) {
			this.err = err;
		}

		@Override
		public void run() {
			boolean flag=true;
			InputStreamReader reader = null;
			try {
				reader = new InputStreamReader(err, "GBK");
			} catch (UnsupportedEncodingException e1) {
				e1.printStackTrace();
			}
			int i;
			while(flag){
				try {
				if((i=reader.read())!=-1)
					System.err.print((char)i);
			
				} catch (IOException e) {
					e.printStackTrace();
					break;
				}
			}
		}
 	}
}

通过java实现windows命令行的衔接


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值