import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 执行命令行命令的工具类,用于执行python程序
* @author ChenRui
*
*/
public class CmdUtils {
public static void cmdExecute(String cmd, String codePath, String input, String output, String arg){
Process process;
StringBuffer sb = new StringBuffer("cmd /c \""); // '/c'不能少
sb.append(cmd+" ");
sb.append(codePath);
sb.append(" -i ");
sb.append(input).append(" -o ").append(output);
if(arg != null && Integer.parseInt(arg) > 0){
sb.append(" -k ").append(arg);
}
sb.append("\"");
try {
Runtime runtime = Runtime.getRuntime();
System.out.println("开始执行...");
process = runtime.exec(sb.toString());
//打印执行的输出结果
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "gbk"); //gbk:解决输出乱码
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
isr.close();
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// cmdExecute("python", "data/k_means.py", "data/788points.txt", "data/out.csv", "5");
cmdExecute("python", "data/cluster_show.py", "data/out.csv", "data/out.png", "5");
}
}
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
/**
* 执行命令行命令的工具类,用于执行python程序
* @author ChenRui
*
*/
public class CmdUtils {
public static void cmdExecute(String cmd, String codePath, String input, String output, String arg){
Process process;
StringBuffer sb = new StringBuffer("cmd /c \""); // '/c'不能少
sb.append(cmd+" ");
sb.append(codePath);
sb.append(" -i ");
sb.append(input).append(" -o ").append(output);
if(arg != null && Integer.parseInt(arg) > 0){
sb.append(" -k ").append(arg);
}
sb.append("\"");
try {
Runtime runtime = Runtime.getRuntime();
System.out.println("开始执行...");
process = runtime.exec(sb.toString());
//打印执行的输出结果
InputStream is = process.getInputStream();
InputStreamReader isr = new InputStreamReader(is, "gbk"); //gbk:解决输出乱码
BufferedReader br = new BufferedReader(isr);
String line;
while ((line = br.readLine()) != null){
System.out.println(line);
}
is.close();
isr.close();
br.close();
}catch (IOException e){
e.printStackTrace();
}
}
public static void main(String[] args) {
// cmdExecute("python", "data/k_means.py", "data/788points.txt", "data/out.csv", "5");
cmdExecute("python", "data/cluster_show.py", "data/out.csv", "data/out.png", "5");
}
}