Prev: C# working with office tools
Next: e-learning question
From: Tony Johansson on 21 Mar 2010 06:44 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
From: Arne Vajhøj on 21 Mar 2010 10:04 On 21-03-2010 06:44, Tony Johansson wrote: > 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
From: Tony Johansson on 21 Mar 2010 11:26 "Arne Vajh�j" <arne(a)vajhoej.dk> skrev i meddelandet news:4ba6274c$0$284$14726298(a)news.sunsite.dk... > On 21-03-2010 06:44, Tony Johansson wrote: >> 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 So BufferedStreams should be used when you don't have an internal buffer that you can increase.? //Tony
From: Arne Vajhøj on 21 Mar 2010 11:46 On 21-03-2010 11:26, Tony Johansson wrote: > "Arne Vajh�j"<arne(a)vajhoej.dk> skrev i meddelandet > news:4ba6274c$0$284$14726298(a)news.sunsite.dk... >> On 21-03-2010 06:44, Tony Johansson wrote: >>> 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. > > So BufferedStreams should be used when you don't have an internal buffer > that you can increase.? Yes. Arne
|
Pages: 1 Prev: C# working with office tools Next: e-learning question |