Is it safe to write more data to a file than space you have on ram?

Let’s say I have 8GB of ram and decided to open a file and over the course of my software’s run I decide to write 16GB of data to the file. Is this safe or could it cause a crash?

If it is safe, how does it work? Does it automatically flush data to file when almost full? Does it store this data in a temporary file?

I don’t see the connection between the RAM and the file size — when you are writing data, the OS should take care of flushing the cache automatically without user intervention.