java - 打开命名管道时 FileInputStream 在构造函数上阻塞

标签 java linux file-io named-pipes fedora

使用 Fedora 12:mkfifo pipe 创建一个管道。

当我使用以下代码尝试针对命名管道打开输入流时,它会在 FileInputStream 构造函数上阻塞,除非我为管道创建写入器,例如打开另一个终端并运行:

tee pipe


public static void main(String[] args){
    try {
        File pipe = new File("/tmp/pipes_debugging/pipeToJava");
        System.out.println( pipe.canRead() );
        FileInputStream fis = new FileInputStream(pipe);
        System.out.println("exiting.");
    } catch (Exception e) {
        e.printStackTrace();
    }
}


输出:

true
<blocks - thread trace shown below>


Thread [main] (Suspended)   
    FileInputStream.open(String) line: not available [native method]    
    FileInputStream.<init>(File) line: 137  
    PipesDebugging.main(String[]) line: 12  

最佳答案

从评论中复制:

这似乎是一种正确的行为。你想做什么? – Banthar 6 月 20 日 7:42

是的,我想这个帖子可能刚刚回答了我的问题:stackoverflow.com/questions/2246862/… - 我正在尝试从管道中读取,我期望流打开并在 read() 上阻塞,而不是在打开流时阻塞。 – 大卫·帕克斯 6 月 20 日 7:52

关于java - 打开命名管道时 FileInputStream 在构造函数上阻塞,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6408099/

相关文章:

java - 接口(interface)内的泛型方法

unit-testing - 包含文件系统调用的单元测试方法

oracle - 导致WRITE_ERROR,Oracle PL/SQL的常见原因是什么?

java - 自签名根 CA 不适用于 Java

java - 当我将 I/O 流包装两次时会发生什么?

java - 如何为 Android 创建 Java 应用程序。问题

php - Linux 高速(/无影响)存储(用于设置)和 PHP

c - 重定向标准流

c++ - 移植Qt程序(Windows到linux)段错误

java - SwingWorker异常