读书人

Java特需的运用

发布时间: 2012-12-25 16:18:29 作者: rapoo

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();
}

}

读书人网 >编程

热点推荐