9.7 Read and Write a Text File


Problem

You need to write data to a sequential text file using ASCII, Unicode, or UTF-8 encoding.

Solution

Create a new System.IO.FileStream object that references the file. To write the file, wrap the FileStream in a System.IO.StreamWriter and use the overloaded Write method. To read the file, wrap the FileStream in a System.IO.StreamReader and use the Read or ReadLine method.

Discussion

.NET allows you to write or read text with any stream by using the StreamWriter and StreamReader classes. When writing data with the StreamWriter , you use the StreamWriter.Write method. This method is overloaded to support all the common C# .NET data types, including strings, chars, integers, floating point numbers , decimals, and so on. However, the Write method always converts the supplied data to text. If you want to be able to convert the text back to its original data type, you should use the WriteLine method to make sure each value is placed on a separate line.

There is more than one way to represent a string in binary form, depending on the encoding you use. The most common encodings include:

  • ASCII, which encodes each character in a string using 7 bits. ASCII- encoded data can't contain extended Unicode characters . When using ASCII encoding in .NET, the bits will be padded and the resulting byte array will have 1 byte for each character.

  • Full Unicode (or UTF-16), which represents each character in a string using 16 bits. The resulting byte array will have 2 bytes for each character.

  • UTF-7 Unicode, which uses 7 bits for ordinary ASCII characters and multiple 7-bit pairs for extended characters. This encoding is primarily for use with 7-bit protocols such as mail, and it isn't regularly used.

  • UTF-8 Unicode, which uses 8 bits for ordinary ASCII characters and multiple 8-bit pairs for extended characters. The resulting byte array will have 1 byte for each character (provided there are no extended characters).

.NET provides a class for each type of encoding in the System.Text namespace. When using the StreamReader and StreamWriter , you can specify the encoding you want to use or simply use the default UTF-8 encoding.

When reading information, you use the Read or ReadLine method of the StreamReader . The Read method reads a single character, or the number of characters you specify, and returns the data as a char or char array. The ReadLine method returns a string with the content of an entire line.

The following Console application shows a simple demonstration that writes and then reads a text file:

 using System; using System.IO; using System.Text; public class TextFileTest {     private static void Main() {         // Create a new file.         FileStream fs = new FileStream("test.txt", FileMode.Create);         // Create a writer and specify the encoding.         // The default (UTF-8) supports special Unicode characters,         // but encodes all standard characters in the same way as         // ASCII encoding.         StreamWriter w = new StreamWriter(fs, Encoding.UTF8);         // Write a decimal, string, and char.         w.WriteLine(124.23M);         w.WriteLine("Test string");         w.WriteLine('!');         // Make sure all data is written from the internal buffer.         w.Flush();         // Close the file.         w.Close();         fs.Close();         Console.WriteLine("Press Enter to read the information.");         Console.ReadLine();         // Open the file in read-only mode.         fs = new FileStream("test.txt", FileMode.Open);         StreamReader r = new StreamReader(fs, Encoding.UTF8);         // Read the data and convert it to the appropriate data type.         Console.WriteLine(Decimal.Parse(r.ReadLine()));         Console.WriteLine(r.ReadLine());         Console.WriteLine(Char.Parse(r.ReadLine()));         r.Close();         fs.Close();         Console.ReadLine();     } } 



C# Programmer[ap]s Cookbook
C# Programmer[ap]s Cookbook
ISBN: 735619301
EAN: N/A
Year: 2006
Pages: 266

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net