我有一个文件generateK.bat
,它随机生成 key 。
我有两个问题:
- 如何在java环境下运行
.bat
文件?我只看到了编辑.bat文件的指令,但没有看到运行它。 - 我想在我的程序中使用该 key 。如何在java中使用输出?
谢谢。
最佳答案
我会使用 Runtime.exec 来执行外部程序: http://docs.oracle.com/javase/1.4.2/docs/api/java/lang/Runtime.html
然后,您将拥有一个带有可以读取/写入的输入和输出流的进程。
代码如下所示:
public static void main(String Argv[]) {
try {
String ls_str;
Process ls_proc = Runtime.getRuntime().exec("pathtoyourbat/generateK.bat");
// get its output (your input) stream
DataInputStream ls_in = new DataInputStream(
ls_proc.getInputStream());
try {
while ((ls_str = ls_in.readLine()) != null) {
System.out.println(ls_str);
}
} catch (IOException e) {
System.exit(0);
}
} catch (IOException e1) {
System.err.println(e1);
System.exit(1);
}
System.exit(0);
}
=> http://www.ensta-paristech.fr/~diam/java/online/io/javazine.html
但是有很多关于在 java 中运行外部应用程序的教程
关于Java - 运行和使用 .bat 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10292847/