Search code examples
c#iofilestreambufferstrategybufferedstream

FileStream and it's buffering strategy


I've found out that there is no point in using BufferedStream in conjunction with FileStream as it has it's own buffering strategy. Yet, I was wondering about one thing:

FileStream fsWithBuffer = new FileStream("buf.dat", FileMode.OpenOrCreate,
                FileAccess.ReadWrite, FileShare.None, 255);
            fsWithBuffer.WriteByte((byte)4);
            fsWithBuffer.Dispose();

This codes write one portion of byte into the specified file. Before that, this byte is kept in inner buffer, so I understand that if I didn't call Dispose() method, nothing would be written to the file.

Now, my question is: sometimes we don't want to put all of the data in a buffer of a FileStream, let's say it is meant to be only for small writes. Is there a possibility to put some data directly into the file associated with FileStream (without putting it earlier into inner buffer)?

Thanks!


Solution

  • To sum up this question, as there was no official answer, here what I was looking for was the Flush() method. It saves the actual buffer to the file associated with FileStream and it was something I needed :)