Java 使用可运行线程读取文件和PipeInputStream

Java 使用可运行线程读取文件和PipeInputStream,java,lambda,runnable,Java,Lambda,Runnable,我正在努力完成一个原本微不足道的家庭作业。然而,因为它可能是如此简单-我没有看到解决办法。这是代码 final Path sourceFilePath = Paths.get(args[0]); final Path sinkFilePath = Paths.get(args[1]); final PipedInputStream pipeInput = new PipedInputStream(10000000); final PipedOutputStream pipeOu

我正在努力完成一个原本微不足道的家庭作业。然而,因为它可能是如此简单-我没有看到解决办法。这是代码

  final Path sourceFilePath = Paths.get(args[0]);
  final Path sinkFilePath = Paths.get(args[1]);

  final PipedInputStream pipeInput = new PipedInputStream(10000000);
  final PipedOutputStream pipeOut = new PipedOutputStream(pipeInput);
        
  final Runnable fileInTransporter = () -> {
        try {
            //TODO one command that reads from the file, and writes into pipe
            pipeOut.write(0);
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            //TODO close related pipe
           pipeOut.close();
        }
    };
        
    final Runnable fileOutTransporter = () -> {
        try {
            //TODO one command reads from pipe, and writes into file
            pipeInput.read();
        } catch (final Exception e) {
            e.printStackTrace();
        } finally {
            //TODO close related pipe
            pipeInput.close();
        }
        };
        
        new Thread(fileInTransporter, "source-transporter-thread").start();
        new Thread(fileOutTransporter, "sink-transporter-thread").start();
    }
我想我对所使用的Runnable接口/lamba实现感到非常困惑。另外,我认为文件sourceFilePath没有读/写到pipedOutputStream中?不知为什么我需要把文件读入管道

我认为管道安装正确。我只是没能把文件读进去


提前谢谢。

我能想出来。 我使用了以下代码:

Files.copy(sourceFilePath, PipeOut);
一开始是可以的

然后,在第二个可运行线程中,我使用

Files.copy(PipeInput, sinkFilePath);

PipeInputStream
是一个玩具。你在这里或其他任何地方都不需要它。我在1998年用过一次,很快就把它拿走了。只需在同一线程中读写即可。