Example
package com.freesamplecode.java.io;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
public class ExecuteDOSCommandDemo {
public static void main(String[] args){
try{
Process process = Runtime.getRuntime().exec("cmd /c dir");
process.waitFor();
BufferedReader br = new BufferedReader(new InputStreamReader(process.getInputStream()));
String line = br.readLine();
while(line != null){
System.out.println(line);
line = br.readLine();
}
System.out.println("done!!");
}catch(IOException ex){
ex.printStackTrace();
}catch(InterruptedException ex){
ex.printStackTrace();
}
}
}
Ouput
Volume in drive D has no label.
Volume Serial Number is F864-472E
Directory of D:\ngoprek\FREESAMPLECODE
09/03/2016 07:47 .
09/03/2016 07:47 ..
09/03/2016 20:03 441 .classpath
07/03/2016 17:41 390 .project
07/03/2016 17:41 .settings
09/03/2016 20:03 bin
08/03/2016 04:37 980 mylog.log.0
09/03/2016 07:47 0 newfile.txt
07/03/2016 17:42 src
4 File(s) 1.811 bytes
5 Dir(s) 34.930.794.496 bytes free
done!!
Screenshot
0 comments: