首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

Java特需的运用

2012-12-25 
Java特需的应用1.在Java中调用操作系统的命令(或者脚本) String commandLine HELPRuntime runtime

Java特需的应用
1.在Java中调用操作系统的命令(或者脚本)
String commandLine = "HELP";
Runtime runtime = Runtime.getRuntime();

try {
Process process = runtime.exec(commandLine);
//OutputStream outputstream = process.getOutputStream();


BufferedReader br = new BufferedReader(
                    new InputStreamReader(process.getInputStream()));
            StringBuilder errorDesc = new StringBuilder();
           
            for (String str = br.readLine(); str != null; str = br
                    .readLine())
            {
                errorDesc.append(str);
            }
           
            System.out.println(errorDesc.toString());
} catch (IOException e) {
e.printStackTrace();
}
2.计时器
public class MyTimer {

private Timer timer;

private static final int STOP_TIME = 3000;

private class SayHello extends TimerTask {

@Override
public void run() {
System.out.println("say hello " + new Date().toLocaleString());
}
}

public void start(){

if ( null == this.timer){
timer = new Timer("Say Hello");
}

SayHello sayHello = new SayHello();

timer.schedule(sayHello, 0, MyTimer.STOP_TIME);
}

public static void main(String[] args){
MyTimer myTimer = new MyTimer();
myTimer.start();
}

}

热点排行