18.4 Reading from and writing to text files


You can use the StreamReader class for reading, and StreamWriter for writing. Unlike the more generalized FileStream class, StreamReader and StreamWriter are specialized classes for text files containing ASCII or Unicode characters . There are useful methods which make text file access more convenient .

18.4.1 Reading using StreamReader

StreamReader has several overloaded constructors but here are the two most useful ones:

  • public StreamReader (string path )

  • public StreamReader (string path, Encoding encoding)

If the first constructor is used, StreamReader will try to determine the file's encoding format from the text file itself. [7]

[7] It is possible to determine if a text file is encoded using Unicode by examining the first few bytes of the file, known as the byte code markers. Text files encoded in the older ASCII format will not have this byte code marker. Windows 9x does not support Unicode, but the other Windows operating systems do.

StreamReader supports the following text encoding formats:

  • ASCII

  • Unicode (both big and little Endian)

  • UTF7

  • UTF8

For the encoding parameter of your second StreamReader constructor, you can pass in either of the following properties of the System.Text.Encoding class:

  • System.Text.Encoding.ASCII

  • System.Text.Encoding.Unicode

  • System.Text.Encoding.UTF7

  • System.Text.Encoding.UTF8

  • System.Text.Encoding.BigEndianUnicode

StreamReader has two methods useful for reading:

  • public override int Read ()

  • public override string ReadLine ()

Read() reads in a single character, and returns the int value of the character. ReadLine() reads the whole line until it reaches a newline character and returns the string or null if the end of file has been reached. ReadLine() does not include the newline character in the returned string.

Here is an example of how StreamReader can be used to read the loveletter.txt file.

 1:  using System;  2:  using System.IO;  3:  4:  public class TestClass{  5:  6:    public static void Main(){  7:      StreamReader sr = new  8:        StreamReader("c:\expt\loveletter.txt");  9: 10:      int counter=0; 11:  string line  =  sr.ReadLine();  12: 13:      while (line!=null){ 14:        counter++; 15:        Console.Write("line #"+counter+" :"); 16:        Console.WriteLine(line); 17:        line = sr.ReadLine(); 18:      } 19: 20:      sr.Close(); 21:    } 22:  } 

Output:

 c:\expt>test line #1 :Dear Juliet, line #2 :I yearn for u! line #3 :Your Romeo 

18.4.2 Writing using StreamWriter

StreamWriter has several overloaded constructors, but here are the two most useful ones:

  • public StreamWriter (string path)

  • public StreamWriter (string path, bool append, Encoding encoding)

If the first constructor is used, StreamWriter will use the UTF8 encoding format by default for writing. If the file indicated by path already exists, it is deleted first (not appended to).

The second constructor takes in a boolean value to indicate if you want to append your new characters ( true ) or overwrite the entire file if it exists ( false ). You can pass in the same encoding parameter as shown in the previous section.

StreamWriter has two methods useful for writing:

  • public override void Write (string value)

  • public override void WriteLine (string value)

The difference between them is that WriteLine appends a newline character automatically to the string. In fact, both methods are overloaded to take in a bool , char , decimal , double , int , long , float , uint , ulong , or char[] instead of a string.

Here is a code fragment showing how StreamWriter can be used:

 80:  StreamWriter sw = new 81:    StreamWriter("c:\expt\reply.txt"); 82: 83:  sw.WriteLine("Dear Romeo,"); 84:  sw.WriteLine("you still owe me cash!"); 85:  sw.WriteLine("Your Juliet"); 86:  sw.Close(); 


From Java to C#. A Developers Guide
From Java to C#: A Developers Guide
ISBN: 0321136225
EAN: 2147483647
Year: 2003
Pages: 221
Authors: Heng Ngee Mok

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