问题描述
在我们的Linux系统上,我们使用命名管道进行进程间通信(生产者和消费者)。
On our Linux system we use named pipes for interprocess communication (a producer and a consumer).
为了测试消费者(Java)代码,我会喜欢实现(在Java中)一个虚拟生成器,它写入连接到使用者的命名管道。
In order to test the consumer (Java) code, I would like to implement (in Java) a dummy producer which writes to a named pipe which is connected to the consumer.
现在测试也应该在Windows开发环境中工作。因此,我想知道如何在Java中从Windows创建命名管道。在Linux中,我可以使用mkfifo(使用 Runtime.exec()
调用),但是我应该如何在Windows上执行此操作?
Now the test should also work in the Windows development environment. Thus I would like to know how to create a named pipe in Windows from Java. In Linux I can use mkfifo (called using Runtime.exec()
), but how should I do this on Windows?
推荐答案
链接中的相关部分
try {
// Connect to the pipe
RandomAccessFile pipe = new RandomAccessFile("\\\\.\\pipe\\testpipe", "rw");
String echoText = "Hello word\n";
// write to pipe
pipe.write ( echoText.getBytes() );
// read response
String echoResponse = pipe.readLine();
System.out.println("Response: " + echoResponse );
pipe.close();
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
这篇关于如何从Java打开Windows命名管道?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!