如何解决使用 Runnable Thread 读取文件和 PipeInputStream
我正在尝试完成一项微不足道的家庭作业。但是,因为它可能很简单 - 我没有看到解决方案。这是代码。
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);
在第一个 Runnable 中
然后,在第二个 Runnable 线程中,我使用了
Files.copy(PipeInput,sinkFilePath);
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。