Closing a stream flushes all application buffers. Whether and when the file actually gets written is up to the OS, but it's guaranteed that if you try to read the file, it'll behave as if it had been written.
This is not necessarily the case if there suddenly was a power failure or a system crash.