# Not able to read a file just created

**URL:** <https://www.ruby-forum.com/t/not-able-to-read-a-file-just-created/161564>\
**Category:** Ruby\
**Created:** [February 27, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/not-able-to-read-a-file-just-created/161564 "2009-02-27T12:56:11Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [February 27, 2009, 12:56pm UTC](https://www.ruby-forum.com/t/not-able-to-read-a-file-just-created/161564/1 "2009-02-27T12:56:11Z")

</div>

hi,

My Ruby code is as follows  
In a class  
One method creats and writes (write\_data) one/more files by reading data  
from another file.  
Another method reads (read\_data) these files and perform some action.

Observed that I am not able to read contents of files just created  
(though I can see data in those files as expected) when I call these  
functions one after the other.

Where as if I run only the ‘Read’ method I am able to read the files.  
There seems to be no error in the code, but I feel that since the file  
is just getting created it is not allowing me to do any action on that  
unless my program is closed and a new program started.

Is there any solution for this? Attached the code for your view.

Naresh

---

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [February 27, 2009, 1:17pm UTC](https://www.ruby-forum.com/t/not-able-to-read-a-file-just-created/161564/2 "2009-02-27T13:17:54Z")

</div>

Try calling File#flush method after writing to the newly created file,  
so that the buffered data is flushed to OS. Or you can even call  
File#fsync to ensure that the data is actually written to disk.

---

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [February 27, 2009, 4:39pm UTC](https://www.ruby-forum.com/t/not-able-to-read-a-file-just-created/161564/3 "2009-02-27T16:39:11Z")

</div>

If these tricks don’t do, try closing the writing filehandle before  
opening  
the reading one, that does it for me.

L-P

2009/2/27 Choi, Junegunn [removed\_email\_address@domain.invalid](mailto:removed_email_address@domain.invalid)

---

<div class="post-metadata">

**Author:** ![naresh](https://www.ruby-forum.com/letter_avatar_proxy/v4/letter/n/c68b51/32.png) [@naresh](https://www.ruby-forum.com/u/naresh)\
**Post date:** [March 3, 2009, 8:26am UTC](https://www.ruby-forum.com/t/not-able-to-read-a-file-just-created/161564/4 "2009-03-03T08:26:22Z")

</div>

Louis-Philippe wrote:

> If these tricks don’t do, try closing the writing filehandle before  
> opening  
> the reading one, that does it for me.

It did work for me too, thanks you very much.

Naresh
