gpt4 book ai didi

java - 如何在 ProcessBuilder 中将文件内容作为参数传递

转载 作者:太空宇宙 更新时间:2023-11-04 08:01:29 25 4
gpt4 key购买 nike

我想将 .dat 文件的内容作为 ProcessBuilder 中的参数传递。我该怎么做?

.dat 文件包含:

08/10/12 4546.4 4644.5 6465.4 3 6.546 core dia,WH,C/C,no of steps,SF 0054.0 0005.0 005.00 0006.0 0006.0 066.00 0006.0 0006.0 006.00 leg width,yoke width,1/2 section step thk-Biggest size

我想在以下代码中将文件的内容作为参数传递

 ProcessBuilder processBuilder = new ProcessBuilder("E:\\MyFile.exe");

最佳答案

FileReader r = null;
try {
r = new FileReader(pathToDatFile);
char[] buf = new char[50000]; // Or whatever is a good max length.
int len = r.read(buf);
String content = new String(buf, 0, len);
String[] params = content.split(" ");
ArrayList<String> invocation = new ArrayList<String>();
invocation.add("E:\\MyFile.exe");
invocation.addAll(Arrays.asList(params));
ProcessBuilder processBuilder = new ProcessBuilder(invocation);
} catch (Exception e) {
// handle me!
} finally {
try { r.close(); } catch (Exception e) { /* handle me! */ }
}

另外:您的 .dat 文件采用什么编码?如果不是 ASCII,则必须通过 FileInputStream -> InputStreamReader,以便可以在 InputStreamReader 中设置正确的编码。否则,您的代码将使用它运行的计算机上的任何默认值,从而产生有趣的不一致结果!

关于java - 如何在 ProcessBuilder 中将文件内容作为参数传递,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12780531/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com