java 如何出现dos窗口_java模拟dos窗口

原来这么简单:

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建没有自动提示功能,要做到这个可能有难度

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值