Reputation: 51
I want to pass a the contents of a .dat file as parameters in ProcessBuilder. How do I do this?
The .dat file contains:
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
I want to pass the content of the file as parameters in following code
ProcessBuilder processBuilder = new ProcessBuilder("E:\\MyFile.exe");
Upvotes: 1
Views: 943
Reputation: 22478
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! */ }
}
Also: what encoding is your .dat file in? If it's not ASCII, you have to go via FileInputStream -> InputStreamReader so you can set the correct encoding in InputStreamReader. Otherwise, your code will use whatever the default is on the computer it happens to run on, with entertainingly inconsistent results!
Upvotes: 1