using BufferReader together wiith StreamReader

T

Tony Johansson

Hi!

When I want to write something to a file I can use a FileStream as the low
level stream that is refering to the file.
From the application side I use a StreamWriter that is encapsulating the
BufferedStream.
The BufferedStream is encapsulating the FileStream that is refering to the
file.
So to be able to eventually increase the performance I could use a
BufferedStream as in this example shows that is buffering the data before it
is written to the actual file.
So the data is not written to the file until you either Close/Flush/dispose
then the data is moved from the BufferedStream to the file.

//Here is a simple example of the usage of the FileStream ,BufferedStream
and StreamWriter
FileStream fs = new FileStream("Test.txt",FileMode.Create);
BufferedStream bs = new BufferedStream(fs);
StreamWriter sw = new StreamWriter(bs);

Now to my question how does read actually work ?
I think it should work something like this.
If you for example read a single character from the file a rather large
chunk of data is then read from the file and
put into the internal default buffer. So the next read from the file will
take the data from the internal buffer instead.
So when all the data in the internal default buffer has been read a new
chunk of data is read from the file and put into the internal default
buffer.
So is it the same here that is I use a BufferedStream when reading data a
larger chunk of data can be taken from the file before a new chunk of data
is taken from the file.

So as a summary it might be a good choice to use BufferedStream when you
read data from a file.to eventually increase the performance.


//Tony
 
A

Arne Vajhøj

When I want to write something to a file I can use a FileStream as the low
level stream that is refering to the file.
From the application side I use a StreamWriter that is encapsulating the
BufferedStream.
The BufferedStream is encapsulating the FileStream that is refering to the
file.
So to be able to eventually increase the performance I could use a
BufferedStream as in this example shows that is buffering the data before it
is written to the actual file.
So the data is not written to the file until you either Close/Flush/dispose
then the data is moved from the BufferedStream to the file.

//Here is a simple example of the usage of the FileStream ,BufferedStream
and StreamWriter
FileStream fs = new FileStream("Test.txt",FileMode.Create);
BufferedStream bs = new BufferedStream(fs);
StreamWriter sw = new StreamWriter(bs);

Now to my question how does read actually work ?
I think it should work something like this.
If you for example read a single character from the file a rather large
chunk of data is then read from the file and
put into the internal default buffer. So the next read from the file will
take the data from the internal buffer instead.
So when all the data in the internal default buffer has been read a new
chunk of data is read from the file and put into the internal default
buffer.
So is it the same here that is I use a BufferedStream when reading data a
larger chunk of data can be taken from the file before a new chunk of data
is taken from the file.

So as a summary it might be a good choice to use BufferedStream when you
read data from a file.to eventually increase the performance.

For reading huge files as lots of small items, then buffering
is good.

But I would use the StreamReader constructor with a buffer size
instead of a BufferedReader.

Arne
 
T

Tony Johansson

Arne Vajhøj said:
For reading huge files as lots of small items, then buffering
is good.

But I would use the StreamReader constructor with a buffer size
instead of a BufferedReader.

Arne

So BufferedStreams should be used when you don't have an internal buffer
that you can increase.?

//Tony
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top