欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

java模拟dos窗口

程序员文章站 2022-05-08 13:30:16
...

原来这么简单:

package runtimeTest;

import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;
import java.util.Scanner;

public class ProcTest {

	/**
	 * @param args
	 */
	public static void main(String[] args) {
		Process process = null;
		try {
			process = Runtime.getRuntime().exec("cmd");
		} catch (IOException e) {
			e.printStackTrace();
		}
		new Thread(new ProcRunnable(process.getInputStream())).start();
		new Thread(new ProcRunnable(process.getErrorStream())).start();

		PrintWriter out = new PrintWriter(process.getOutputStream());
		java.util.Scanner in = new java.util.Scanner(System.in);

		while(in.hasNextLine()){
			out.println(in.nextLine());
			out.flush();

		}
	}
}
class ProcRunnable implements Runnable{

	InputStream inputStream = null;
	String name;
	public ProcRunnable(InputStream inputStream){
		this.inputStream = inputStream;
	}
	@Override
	public void run() {
		Scanner scanner = new Scanner(inputStream);
		while(scanner.hasNextLine()){
			System.out.println(scanner.nextLine());
		}
		try {
			inputStream.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
		scanner.close();
	}
}

这种方法最直接,但是没有下一行时,scanner.hasNextLine()会阻塞在那边,直到下一个换行符。
所以这样感觉和dos窗口有少许区别:dos窗口有一个如“c:\Users\macondo>_”这样的东西提示你输入下一个命令;而以上程序效果是你输入命令后那个提示才出来。原因是那个提示后面没有换行符,等到有换行符时才会被输出。

下面是一种解决方法:用timer定时检查inputstream,有东西就把它输出来:
package cmd;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.IOException;
import java.io.InputStream;
import java.io.PrintWriter;

public class cmd {
	public static void main(String...args) {
		Process process = null;
		PrintWriter writer = null;
		
		final byte[] b = new byte[1024];
		try {
			process = Runtime.getRuntime().exec("cmd");
			writer = new PrintWriter(process.getOutputStream());
			
		} catch (IOException e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}

		final InputStream inputStream = process.getInputStream();
		final InputStream err = process.getErrorStream();
		java.util.Scanner scanner = new java.util.Scanner(System.in);
		javax.swing.Timer timer = new javax.swing.Timer(500,new ActionListener() {
			
			@Override
			public void actionPerformed(ActionEvent e) {
				// TODO Auto-generated method stub
				try{
					while(inputStream.available()>0){
						int c = inputStream.read(b,0,b.length);
						System.out.print(new String(b,0,c));
					}
				}catch (Exception ee) {
					// TODO: handle exception
				}
				try{
					while(err.available()>0){
						int c = err.read(b,0,b.length);
						System.out.print(new String(b,0,c));
					}
				}catch (Exception ee) {
					// TODO: handle exception
				}
			}
		});
		timer.start();
		while(scanner.hasNextLine()){			
			writer.println(scanner.nextLine());
			writer.flush();
		}
	}
}

以上两种方法都有一个和dos不同的地方,因为在同一个console里面输入,你输入一个命令,outputstream又会把那个命令重复输出一遍。这个好解决,不用让它输出到console,做个jframe,从jtextfield里输入,输出到jtextarea就行了

另外,按下tab建没有自动提示功能,要做到这个可能有难度

转载于:https://my.oschina.net/soitravel/blog/35126