1

我想将 .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");
4

1 回答 1

1
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 中设置正确的编码。否则,您的代码将使用它所运行的计算机上的任何默认值,而结果却非常不一致!

于 2012-10-08T11:25:49.493 回答