【问题标题】:one stdout to one stdin from java to python从 java 到 python 的一个标准输出到一个标准输入
【发布时间】:2015-02-05 00:56:04
【问题描述】:

我有这个java代码

public static main void (String[] args)
{
    System.out.println("this is iteration 1");
    System.out.println("this is iteration 2");
    System.out.println("this is iteration 3");
    System.out.println("this is iteration 4");
}

Python

import sys 

try:
    while True:
        data = raw_input()
        print "in python " + data
except:
    print error

期望的输出

In python : this is iteration 1
In python : this is iteration 2
In python : this is iteration 3
In python : this is iteration 4

电流输出

In python : this is iteration 1
In python : this is iteration 2
In python : this is iteration 3
In python : this is iteration 4
Error

基本上它的作用是能够打印出从标准输出到标准输入从java到python的四行。但是,当标准输出完成时,我希望它被阻塞而不是再次运行。

我的第二个问题是,system.out.println 是全部通过管道传输到 stdout 缓冲区并且 stdin 读取它们还是逐行读取?

我运行代码的命令是这样的

java -jar stdOut.jar | python testStdin.py

【问题讨论】:

    标签: java python stdout stdin


    【解决方案1】:

    几乎所有事情都如您所愿。查看EOFError 了解为什么会发生这种情况。

    import sys 
    
    try:
        while True:
            data = raw_input()
            print "in python " + data
    except EOFError:
        pass
    

    所以基本上你正在阅读,直到到达流的末尾。如果您希望输入阻塞,输出也应该保持打开状态。

    对于第二个问题,只需在中间某处延迟输出一小会儿即可。在等待其余部分时,您已经看到了第一行。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-12-13
      • 1970-01-01
      • 2021-12-13
      • 1970-01-01
      • 1970-01-01
      • 2015-06-01
      相关资源
      最近更新 更多