runtime.getruntime (). EXEC (COMMANDSTR)Method provides a call to execute the cmd command
cmd/c dir closes the command window after executing the dir command.
cmd/k dir does not close the command window after executing the dir command.
cmd/c start dir will open a new window and execute the dir instruction, the original window will be closed.
cmd/k start dir will open a new window and execute the dir instruction, the original window will not be closed.
Cases
①
public class Test1 {
public static void Main (string[] args) {
try{
Runtime.getruntime (). EXEC ("cmd.exe/c move e:\\xxx\\1 E:");
}catch (Exception e) {
E.printstacktrace ();
The Printstacktrace () method means that you print the location of the exception information in the program and the cause of the error in the command line.
}
}
}
Ii
public class Test1 {
public static void Main (string[] args) {
Runtime run = Runtime.getruntime ();
try{
Process process = Run.exec ("cmd.exe/c move e:\\xxx\\1 E:");
}catch (Exception e) {
System.out.println ("Error");
}
}
}
read the contents of the CMD output:
①//java Program through Java.lang.Process.getInputStream () to obtain the results of CMD execution information;
②//inputstreamreader () is a bridge between a byte stream and a character stream, which outputs a byte stream and can specify a character set for the byte stream, which outputs a
The character of the other;
③//bufferedreader () provides a common buffered text read, ReadLine reads a line of text, reads text from the character input stream, buffers individual characters, and provides efficient reading of characters, arrays, and rows.
Cases
try {
Process p = runtime.getruntime (). EXEC ("cmd.exe/c dir e:\\xxx");
InputStream instream = P.getinputstream ();
InputStreamReader inreader = new InputStreamReader (instream);
BufferedReader inbuffer = new BufferedReader (inreader);
String s;
While ((s = inbuffer.readline ()) = null){
System.out.println (s);
}
} catch (IOException e) {
E.printstacktrace ();
}
Java invoke cmd command