gpt4 book ai didi

java - BufferedReader - 打印整个输出

转载 作者:行者123 更新时间:2023-12-02 05:55:09 25 4
gpt4 key购买 nike

我正在尝试用 Java 打印 python 文件的输出。

Python 文件基本上是一个网络抓取工具 - 其中有多个函数,每个函数都包含一些输出行。因此输出应该按顺序出现,一个接一个。每行输出必须以固定的间隔出现(根据代码),而不是全部出现在一起。下面的代码打印输出,但只有在整个程序运行之后,我才能将输出作为单个文本获得。

public class Samtest extends Thread {

public static void main(String[] args) {

String pythonScriptPath0 = "IR_Component_module11.py";
String[] cmd0 = new String[2];
cmd0[0] = "C:\\Python27\\python.exe";
cmd0[1] = pythonScriptPath0;
Runtime rt0 = Runtime.getRuntime();
java.lang.Process pr0 = rt0.exec(cmd0);
BufferedReader bfr0 = new BufferedReader(new InputStreamReader(pr0.getInputStream()));
String line0 = "";
while((line0 = bfr0.readLine()) != null)
{
System.out.println(line0);
}

}
}

我需要按照程序运行自然地输出。但程序运行后我得到了完整的输出。刮刀由 GUI 组成。因此,只有当我退出 GUI 时,我才能得到输出。

最佳答案

编辑

Python 缓冲输出。使用-u

import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;

public class Samtest extends Thread {

public static void main(String[] args) throws IOException {

String pythonScriptPath0 = "IR_Component_module11.py";
String[] cmd0 = new String[3];
cmd0[0] = "C:\\Python27\\python.exe";
cmd0[1] = "-u";
cmd0[2] = pythonScriptPath0;
Runtime rt0 = Runtime.getRuntime();
java.lang.Process pr0 = rt0.exec(cmd0);
BufferedReader bfr0 = new BufferedReader(new InputStreamReader(pr0.getInputStream()));
String line0 = "";
while ((line0 = bfr0.readLine()) != null) {
System.out.println(line0);
}

}
}

关于java - BufferedReader - 打印整个输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23174151/

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