为什么FileInputStream的read()没有被阻塞? [英] Why is the FileInputStream read() not blocking?

查看:153
本文介绍了为什么FileInputStream的read()没有被阻塞?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个Writer程序,将一行文本写入文件,然后等待用户在写入另一行之前返回,然后退出。只有在此之后,文件关闭。代码:

  public class Writer {

Writer(){
}

public static String [] strings =
{
Hello World,
Goodbye World
};

public static void main(String [] args)
抛出java.io.IOException {

java.io.FileOutputStream pw =
new java .io.FileOutputStream( myfile.txt的);

(String s:strings){
pw.write(s.getBytes());
System.in.read();
}

pw.close();




$ b $ p
$ p

b


java Writer

然后我也有一个读者程序,以及我期望的)块,只要文件的写入还没有完成(即pw.close()尚未被调用)。代码:

  public class ReaderFIS extends Object {

ReaderFIS(){
}

public static void main(String [] args)throws Exception {

java.io.FileInputStream(myfile.txt);

int ch = -1; ((ch = in.read())> = 0){
System.out.println(ch =+ ch);
}
System.out.println(Last ch =+ ch);

System.out.println(exiting);




$ b

开始于:


java ReaderFIS


现在我期待read()第一个Hello World文本,基于Javadoc文档:
$ b


从这个输入流中读取一个字节的数据。如果没有输入可用,则此方法将被阻止。
通过: http: //docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html#read()


但ReaderFIS在阅读Hello World之后立即完成,显然看到EOF!所以它不是块!它转储的字符值,然后一个-1,然后打印退出。

输出:
ch = 72
ch = 101
ch = 108
ch = 108
ch = 111
ch = 32
ch = 87
ch = 111
ch = 114
ch = 108
ch = 100
Last ch = -1
exiting

我试过的其他变化是:通过getChannel (),通过getChannel()检查是否可以lock()ed,使用available(),使用缓冲区尝试read(),尝试readLine(),在每次写入之间连续写入一个500ms暂停的文件,不要写任何东西只是保持打开文件的文件。

这些变化都没有导致ReaderFIS程序阻止,它总是完成。

读者程序为什么不阻止?我错过了一些非常明显的东西吗?看来ReaderFIS程序找到一个EOF(-1),但为什么呢?这个文件还没有被Writer程序关闭。

Funnysidenote:System.in.read()被拦截! (并等待用户点击Enter)。



PS:在Windows XP和Suse Linux上尝试过。在Windows上,我无法删除文件,而作家正在运行(这是我所期望的)。



问候,
Marco

$ b FileInputStream始终有可用的输入:要么有剩余的字节要读取,要么有EOF,但是一般来说读取时不会阻塞。您可以在下列情况下获得封锁:
$ b $ ul
  • 从控制台/终端读取
  • 网络

  • 从管道读取
  • 从等待的数据流中读取数据。

    $ b

    文件流不需要等待数据,因为它们总是有数据可用:在你的情况下 read()将获得,基本上是随机的:


    • 旧版本的文件

    • 文件的新版本

    • 文件的半升级版本。


    I've got a Writer program that writes one line of text to a file, then waits until the user hits return before it writes another line and then exits. Only after that is the file closed. The code:

    public class Writer {
    
        Writer() {
        }
    
        public static String[] strings = 
            {
                "Hello World", 
                "Goodbye World"
            };
    
        public static void main(String[] args) 
            throws java.io.IOException {
    
            java.io.FileOutputStream pw =
                new java.io.FileOutputStream("myfile.txt");
    
            for(String s : strings) {
                pw.write(s.getBytes());
                System.in.read();
            }
    
            pw.close();
        }
    }
    

    Start first with:

    java Writer

    Then I also have a reader program that should (well I expected) block as long as the writing of the file hasn't finished yet (i.e pw.close() has not been called yet). The code:

    public class ReaderFIS extends Object {
    
        ReaderFIS() {
        }
    
        public static void main(String[] args) throws Exception {
    
            java.io.FileInputStream in = new java.io.FileInputStream("myfile.txt");
    
            int ch = -1;
            while((ch = in.read()) >= 0) {
             System.out.println("ch = " + ch);
         }
            System.out.println("Last ch = " + ch);
    
         System.out.println("exiting");
        }
    }
    

    Start with:

    java ReaderFIS

    Now I expected the read() to block after reading the first "Hello World" text, based on this in the Javadoc documentation:

    Reads a byte of data from this input stream. This method blocks if no input is yet available. Via: http://docs.oracle.com/javase/6/docs/api/java/io/FileInputStream.html#read()

    But the ReaderFIS is immediately done after reading "Hello World" and apparently sees an EOF! So it does not block! It dumps the character values, then a -1 and then prints "exiting".

    Output: ch = 72 ch = 101 ch = 108 ch = 108 ch = 111 ch = 32 ch = 87 ch = 111 ch = 114 ch = 108 ch = 100 Last ch = -1 exiting

    Other variations I tried were: reading via a getChannel(), checking via getChannel() if it can be lock()ed, using available(), trying read() using a buffer, trying readLine(), continously writing a character in the file with a 500msec pause in between each write, not writing anything just keeping the file open in the Writer.
    None of these variations cause the ReaderFIS program to block, it always finishes.

    Why does the reader program not block? Did I miss something soooo very obvious? It seems the ReaderFIS program finds an EOF (-1) but why? The file has not been closed yet by the Writer program.

    "Funny" sidenote: the System.in.read() is blocking! (and waiting for the user to hit Enter).

    PS: tried this on Windows XP and Suse Linux. On Windows I can't delete the file while the writer is running (which is as I expected).

    Regards, Marco

    解决方案

    FileInputStream always has input available: either there are bytes left to read or there is an EOF, but in general it will not block when reading. You can get blocked when you are:

    • reading from a console / terminal
    • reading from the network
    • reading from a pipe
    • reading from whatever stream that is waiting for data.

    File Streams do not have to wait for data as they always have data available: in your case read() will get, basically at random, one of:

    • the old version of the file
    • the new version of the file
    • half-updated version of the file.

    这篇关于为什么FileInputStream的read()没有被阻塞?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

  • 查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆