Emi*_*mya 5 java linux terminal flush
1)我使用Java调用Linux终端来运行foo.exe并将输出保存在文件中:
String[] cmd = {"/bin/sh", "-c", "foo >haha.file"};
Runtime.getRuntime().exec(cmd);
Run Code Online (Sandbox Code Playgroud)
2)问题是当我打算稍后在代码中阅读haha.file时,它尚未编写:
File f=new File("haha.file"); // return true
in = new BufferedReader(new FileReader("haha.file"));
reader=in.readLine();
System.out.println(reader);//return null
Run Code Online (Sandbox Code Playgroud)
3)只有在程序完成后才会写入haha.file.我只知道如何冲洗"作家",但不知道如何冲洗...... 像这样.如何强制java在终端中写入文件?
在此先感谢EE
您可以等待该过程完成:
Process p = Runtime.getRuntime().exec(cmd);
int result = p.waitFor();
Run Code Online (Sandbox Code Playgroud)
或者使用 p.getInputStream() 直接从进程的标准输出读取。