我需要將 Java 輸出保存到 test2.txt 檔案,運行 java 編譯的 haskell hello.exe 檔案,從 tes2.txt 獲取資料,用它做一些事情,然后將結果輸出到控制臺并用 Java 讀取它。
你好.exe
import System.IO
import Control.Monad
import Data.List
main = do
handle <- openFile "test2.txt" ReadMode
contents <- hGetContents handle
let sas = words contents
putStrLn (unwords sas)
爪哇
import java.io.*;
public class Main {
public static void main(String[] args) throws IOException {
String str = "1 13 23";
BufferedWriter writer = new BufferedWriter(new FileWriter("C:\\Users\\dabch\\OneDrive\\Desktop\\chepuch\\chich\\test2.txt"));
writer.write(str);
writer.close();
Process p = Runtime.getRuntime().exec("C:\\Users\\dabch\\OneDrive\\Desktop\\chepuch\\chich\\hello.exe");
String s;
System.out.println(p.getOutputStream());
BufferedReader stdInput = new BufferedReader(new
InputStreamReader(p.getInputStream()));
while((s=stdInput.readLine())!=null){
System.out.println(s);
}
}
}
當我自己運行 hello.exe 時,它??給了我正確的結果:1 13 23。
但是當我使用 Java getRuntime().exec() 運行 hello.exe 時,它??給了我:1 1 1。
我怎樣才能解決這個問題?
uj5u.com熱心網友回復:
“hello.exe”的當前目錄與 java 應用程式相同,其中可能包含一個名為“text2.txt”的檔案,內容為“1 1 1”。這與您撰寫的檔案不同,即C:\Users\dabch\OneDrive\Desktop\chepuch\chich\test2.txt.
通過使 hello.exe 讀取C:\Users\dabch\OneDrive\Desktop\chepuch\chich\test2.txt或更改子行程 hello.exe 的目錄進行修復,使其從以下位置運行C:\Users\dabch\OneDrive\Desktop\chepuch\chich:
File exe = new File("C:\\Users\\dabch\\OneDrive\\Desktop\\chepuch\\chich\\hello.exe")
Process p = new ProcessBuilder(exe.toString())
.directory(exe.getParentFile())
.inheritIO().start();
有了.inheritIO()你,你應該直接看到輸出而沒有你的回圈。
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/523574.html
標籤:爪哇哈斯克尔
