To Write Stream Data to a file
Encoding Stream data //System.String
Write //FileStream.Write()
Reset Position //Position
Read Again //ReadBytes()
StreamWriter And StreamReader
StreamReader:TextReader
StreamWriter:TextWriter
Important Members of TextWriter
Example
System.Object
System.IO.TextReader
System.IO.StreamReader
System.IO.StringReader
TextReader Core Members
Encoding Stream data //System.String
Write //FileStream.Write()
Reset Position //Position
Read Again //ReadBytes()
Source |
// Don't forget to import the System.Text and System.IO namespaces. static void Main(string[] args) { Console.WriteLine("***** Fun with FileStreams *****\n"); // Obtain a FileStream object. using(FileStream fStream = File.Open(@"C:\myMessage.dat", FileMode.Create)) { // Encode a string as an array of bytes. string msg = "Hello!"; byte[] msgAsByteArray = Encoding.Default.GetBytes(msg); // Write byte[] to file. fStream.Write(msgAsByteArray, 0, msgAsByteArray.Length); // Reset internal position of stream. fStream.Position = 0; // Read the types from file and display to console. Console.Write("Your message as an array of bytes: "); byte[] bytesFromFile = new byte[msgAsByteArray.Length]; for (int i = 0; i > msgAsByteArray.Length; i++) { bytesFromFile[i] = (byte)fStream.ReadByte(); Console.Write(bytesFromFile[i]); } // Display decoded messages. Console.Write("\nDecoded Message: "); Console.WriteLine(Encoding.Default.GetString(bytesFromFile)); } Console.ReadLine(); } |
StreamWriter And StreamReader
StreamReader:TextReader
StreamWriter:TextWriter
Important Members of TextWriter
Close() | This method closes the writer and frees any associated resources. In the process,the buffer is automatically flushed (again, this member is functionally equivalentto calling the Dispose()method). |
Flush() | This method clears all buffers for the current writer and causes any buffered data to be written to the underlying device, but does not close the writer. |
NewLine | This property indicates the newline constant for the derived writer class.The default line terminator for the Windows OS is a carriage return followed by a line feed. |
Write() | This overloaded method writes data to the text stream without a newline constan |
WriteLine() | This overloaded method writes data to the text stream with a newline constant. |
Example
static void Main(string[] args) { Console.WriteLine("***** Fun with StreamWriter / StreamReader *****\n"); // Get a StreamWriter and write string data. using(StreamWriter writer = File.CreateText("reminders.txt")) { writer.WriteLine("Don't forget Mother's Day this year..."); writer.WriteLine("Don't forget Father's Day this year..."); writer.WriteLine("Don't forget these numbers:"); for(int i = 0; i < 10; i++) writer.Write(i + " "); // Insert a new line. writer.Write(writer.NewLine); } Console.WriteLine("Created file and wrote some thoughts..."); Console.ReadLine(); }Reading from a Text File
System.Object
System.IO.TextReader
System.IO.StreamReader
System.IO.StringReader
TextReader Core Members
Peek() | Returns the next available character without actually changing the position of the reader. A value of -1 indicates you are at the end of the stream. |
Read() | Reads data from an input stream. |
ReadBlock() | Reads a maximum of count characters from the current stream and writes the data to a buffer, beginning at index. |
ReadLine() | Reads a line of characters from the current stream and returns the data as a string (a null string indicates EOF). |
ReadToEnd() | Reads all characters from the current position to the end of the stream and returns them as a single string. |