Is a Java FileInputStream file lock file for writing

I create a quick bridge between two separate programs.

One program writes to a file, and my program reads it at the same time.

It seems that as soon as I call the first read, the record is completely locked.

Why is this happening and what is the workaround?

// Create Named Pipe Runtime.getRuntime.exec("mkfifo /tmp/mypipe") val stream = new FileInputStream("/tmp/mypipe") val in = new BufferedReader(new InputStreamReader(stream)) // File opened for reading. No blocking at this point. while (true) { println(in.readLine()) // File read. Now blocking. } 

Update:

This file is actually a named pipe created with mkfifo /tmp/mypipe . I tried using this with a regular File , and it worked just fine - all the data displayed.

I use a named pipe because I do not need IO overhead on disk.

+5
source share
1 answer

If I had to bet, I would say that this is a buffering problem.

Initially, I thought that BufferedReader might try to fill its entire buffer, but it looks happy while it reads something. See the fill() method.

If you use readLine() , the question also arises whether the input you receive contains new lines.

Another thing that can happen, depending on how much the original program produces, is related to channel buffering. Did this answer help ? You can also try completing the original program (e.g. ^C or one), and then see if the destination program prints anything.

This page shows that buffering can be a problem:

However, if buffered writing is used, the buffer is not provided until the buffer is read. This flushing occurs .s when more data is written to the buffer than the maximum buffer size (BUFSIZ is set to stdio.h), or when the pipe is closed by the writer.

+6
source

All Articles