17.3.1 按文本模式讀寫
SteamReader和StreamWriter類為我們提供了按文本模式讀寫數據的方法。下面的例子是從一個文本文件中讀取內容並打印在屏幕上。
程序清單17-5:
using System; using System.IO; class FileTest { public static void Main() { streamReader srd; try{ srd=File.OpenText("c:\\C#\\file1\\file1.txt"); } catch { Console.WriteLine("File open failed"); return; } while(srd.Peek()!=-1) { String str=srd.ReadLine(); Console.WriteLine(str); } Console.WriteLine("The end of the file has been reached"); srd.Close(); } }
上面用到的方法OpenText是File類的一個靜態方法,不能被某個具體的File類的實例調用。它表示從一個已經存在的文本文件中讀取一個文本流,並保存在一個StreamReader實例中。下面的例子則是向文本文件中寫入文本流。
程序清單17-6:
using System; using System.IO; class FileTest { public static void Main() { StreamWriter srd; try{ srd=File.CreateText("c:\\C#\\file1\\file2.txt"); } catch { Console.WriteLine("File create failed"); return; } srd.WriteLine("Web Address:"); srd.WriteLine("www.bianceng.cn"); srd.WriteLine("www.263.net"); srd.writeLine("www.microsoft.com/china"); srd.WriteLine("www.sina.com.cn"); srd.Close(); } }
17.3.2 按二進制模式讀寫
System.IO還為我們提供了BinaryReader和BinaryWriter類,用於按二進制模式讀寫文件。它們提供的一些讀寫方法是對稱的,比如針對不同的數據結構,BinaryReader提供了ReadByte,ReadBoolean,ReadInt,ReadInt16,ReadDouble,ReadString等方法,而BinaryWriter則提供了WriteByte,WriteBoolean,WriteInt,WriteInt16,WriteDouble,WriteString方法。
下面我們舉一個通訊錄的例子來說明BinaryReader和BinaryWriter類是如何使用的。
程序清單17-7:
using System; using System.IO; struct Record{ public string name; public UInt16 age; public string phone; public string address; }; //通訊錄的記錄格式 class PhoneBook { FileStream f_srm; string s_filename; Record m_record; public PhoneBook(string filename) { s_filename=filename; } public int Open() { //顯示初始化界面 Console.WriteLine("1:List Record"); Console.WriteLine("2:Add New Record"); Console.WriteLine("3:Erase Record"); Console.WriteLine("0:Exit"); int i=Console.Read()-'0'; return i; } public void AddNew() { //向通訊錄中添加記錄 try{ f_srm=new FileStream(s_filename,FileMode.OpenOrCreate); } catch { Console.WriteLine("PhoneBook Error!"); return; } BinaryWriter pw=new BinaryWriter(f_srm); pw.Seek(0,SeekOrigin.End); string s_temp; Console.Write("Name:"); Console.ReadLine(); m-record.name=Console.ReadLine(); pw.WriteString(m_record.name); reenter: Console.Write(Age:"); s_temp=Console.ReadLine(); try{ m_record.age=s_temp.ToUInt16(); } catch { Console.WriteLine("The format of age is error,please ReEnter"); goto reenter; } pw.Write(m_record.age); Console.Write("Phone Number:"); m_record.phone=Console.ReadLine(); pw.WriteString(m_record.phone); Console.Write("Address:"); m_record.address=Console.ReadLine(); pw.WriteString(m_record.address); f_srm.Close(); } public void List() { //浏覽所有記錄 try{ f_srm=new FileStream(s_filename,FileMode.Open); } catch { Console.WriteLine("PhoneBook Error!"); return; } BinaryReader pr=new BinaryReader(f_srm); Console.WriteLine("Name Age Phone Number Address"); Console.WriteLine("*********************************"); while(true) { try{ m_record.name=pr.ReadString(); m_record.age=pr.ReadUInt16(); m_record.phone=pr.ReadString(); m_record.address=pr.ReadString(); } catch{return;} Console.WriteLine("{0,-9}{1,-6}{2,-18}{3}",m_record.name, m_record.age,m_record.phone,m_record.address); } f_srm.Close(); } public void Erase() { //清空所有記錄 try{ f_srm=new FileStream(s_filename,FileMode.Create); } catch { Console.WriteLine("PhoneBook Error!"); return; } f_srm.Close(); Console.WriteLine("Now the book is empty!"); } } class Test { public static void Main() { PhoneBook pbook=new PhoneBook("c:\\C#\\Phone.book"); int i=pbook.Open(); switch(i) { case 1: pbook.List(); break; case 2: pbook.AddNew(); break; case 3: pbook.Erase(); } } } 其中有這麼一條語句: Console.WriteLine("{0,-9}{1,-6}{2,-18}{3}",m_record.name, m_record.age,m_record.phone,m_record.address);
它表示對輸出進行格式化。“{}”中使用的第二個數表示輸出占據的屏幕位數。正數表示從右邊起計,而負數表示從左邊起計。