简体   繁体   English

如何通过 DataOutputStream 发送算术运算的有效结果

[英]How to send a valid result of an arithmetic operation through DataOutputStream

I have a subprocess class that calculate the sum of two integers and then put it in a DataOutputStream:我有一个子进程 class 计算两个整数的总和,然后将其放入 DataOutputStream:

public class SubProcess {
  public static void main(String[] args) throws IOException {
     DataInputStream in = new DataInputStream(System.in);
     DataOutputStream out = new DataOutputStream(System.out);
     int a = in.readInt();
     out.writeInt(a);
     int b = in.readInt();
     out.writeInt(b);
     int result = a+b;
     out.writeInt(result);
     out.flush();
     in.read();
     out.close();
     in.close();
    }
  } 

when writing the two values of a and b like 12 and 47 respectivley the result is "ei".当分别写入 a 和 b 的两个值(如 12 和 47)时,结果为“ei”。

In another hand the mainprocess won't read that result like an DataInputStream through the ReadInt() line, and it throws an exception:另一方面,主进程不会像 DataInputStream 那样通过 ReadInt() 行读取该结果,它会引发异常:

        Exception in thread "main" java.io.EOFException
            at java.io.DataInputStream.readInt(Unknown Source)
            at testthread.MainProcess.main(MainProcess.java:21)

It seems that the main process doesn't communicate really with the subprocess.似乎主进程并没有真正与子进程通信。 deleting the package from the two classes and running the main process on cmd avoid the exception.从两个类中删除 package 并在 cmd 上运行主进程避免异常。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM