diff options
author | chai <215380520@qq.com> | 2023-05-12 09:24:40 +0800 |
---|---|---|
committer | chai <215380520@qq.com> | 2023-05-12 09:24:40 +0800 |
commit | 2a1cd4fda8a4a8e649910d16b4dfa1ce7ae63543 (patch) | |
tree | a471fafed72e80b4ac3ac3002e06c34220dd6058 /ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md | |
parent | b8a694746562b37dc8dc5b8b5aec8612bb0964fc (diff) |
*misc
Diffstat (limited to 'ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md')
-rw-r--r-- | ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md | 29 |
1 files changed, 29 insertions, 0 deletions
diff --git a/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md new file mode 100644 index 0000000..a470129 --- /dev/null +++ b/ThirdParty/CsvHelper-master/src/CsvHelper.Website/input/examples/prerequisites/streams/index.md @@ -0,0 +1,29 @@ +# Streams + +When reading from a stream, if you need to go back to the beginning of the stream, you can use the `Stream.Position` property. + +```cs +using (var stream = new File.OpenRead("path\\to\\file")) +using (var reader = new StreamReader(stream)) +{ + // Read file content. + var content = reader.ReadToEnd(); + + // Go back to beginning of the stream. + stream.Position = 0; + + // Read file content again. + content = reader.ReadToEnd(); +} +``` + +When writing to a file, you need to flush the writer for the data to be written to the stream. `StreamWriter` contains an internal buffer and the data is only written to the stream when the buffer is full, or `Flush` is called. `Flush` is automatically called when a `using` block exits. + +```cs +using (var stream = new File.OpenWrite("path\\to\\file")) +using (var writer = new StreamWriter(stream)) +{ + writer.WriteLine("Foo"); + writer.Flush(); // Data is written from the writer buffer to the stream. +} // Flush is also called here. +``` |