Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java FileOutputStream when will written bytes be ready for read? [duplicate]

I am using a file as a cache for big data. One thread writes to it sequentially, another thread reads it sequentially.

Can I be sure that all data that has been written (by write()) in one thread can be read() from another thread, assuming a proper "happens-before" relationship in terms of the Java memory model? Is this behavior documented?

In my JDK, FileOutputStream does not override flush(), and OutputStream.flush() is empty. That's why I'm wondering...

The streams in question are owned exclusively by a class that I have full control of. Each stream is guaranteed to be accesses by one thread only. My tests show that it works as expected, but I'm still wondering if this is guaranteed and documented.

See also this related discussion.

like image 750
krlmlr Avatar asked Oct 05 '12 07:10

krlmlr


People also ask

How does FileOutputStream work Java?

Creates a file output stream to write to the file with the specified name. If the second argument is true , then bytes will be written to the end of the file rather than the beginning. A new FileDescriptor object is created to represent this file connection.

Where does FileOutputStream write to?

FileOutputStream(File file) Creates a file output stream to write to the file represented by the specified File object. FileOutputStream(File file, boolean append) Creates a file output stream to write to the file represented by the specified File object.

When writing data to a file using a FileOutputStream At what point is the data actually written to the file?

To write data to a Java FileOutputStream you can use its write() method. The write() method takes an int which contains the byte value of the byte to write. Thus, only the lower 8 bit of the passed int actually gets written to the FileOutputStream destination.


2 Answers

Assuming you are using a posix file system, then yes.

FileInputStream and FileOutputStream on *nix use the read and write system calls internally. The documentation for write says that reads will see the results of past writes,

After a write() to a regular file has successfully returned:

Any successful read() from each byte position in the file that was modified by that write shall return the data specified by the write() for that position until such byte positions are again modified.

I'm pretty sure ntfs on windows will have the same read() write() guarantees.

like image 188
sbridges Avatar answered Oct 05 '22 23:10

sbridges


You can't talk about "happens-before" relationship in terms of the Java memory model between your FileInputStream and FileOutputStream objects since they don't share any memory or thread. VM is free to reorder them just honoring your synchronization requirements. When you have proper synchronization between reads and writes without application level buffering, you are safe.

However FileInputStream and FileOutputStream share a file, which leaves things up to the OS which in main stream ones you can expect to read after write in order.

like image 44
auselen Avatar answered Oct 06 '22 01:10

auselen