需要切换到对应文件夹下,执行文件时使用
/**
* 通过文件夹与文件名调用cmd文件
* @param filePath
* @param fileName
* @return
*/
public static int encrypt(String filePath,String fileName){
int waitFor = Integer.MIN_VALUE;
String cmd = filePath +"/"+fileName;
File location =new File(filePath +"/");
Process process = null;
//waitFor具体含义:https://blog.csdn.net/qq_35661171/article/details/79096786
try {
process = Runtime.getRuntime().exec(cmd, null, location);
waitFor = process.waitFor();
System.err.println("waitFor="+waitFor);
InputStream in = process.getInputStream();
int length = in.available();
byte[] out = new byte[length];
in.read(out, 0, length);
System.out.println(new String(out, "gbk"));
in.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
process.exitValue();
return waitFor;
}
Runtime.getRuntime().exec(cmd, null, location);其中null,为环境变量,为null则会继承当前线程的环境变量。
waitfor是用来判断指令执行结果的。
inputStrean获取指令的输出流。