把握VB.NET中的流(Stream) (三)

來源:互聯網
上載者:User
 

檔案操作具體執行個體在這一部分,你將找到更多常用的檔案操作的代碼執行個體。最常用、最基本的操作就是把text寫入檔案和讀回來。現在的應用程式通常不用二進位檔案作儲存簡單的變數,而用它來儲存物件,對象集合以及其他機器代碼。下面,將看到具體操作的例子。 讀寫文字檔為了把text儲存到檔案,建立一個基於FileStream的StreamReader對象,然後調用Write方法把需要儲存的text寫入檔案。下面的代碼用SaveFileDialog提示使用者指定一個檔案,用於儲存TextBox1的內容。    SaveFileDialog1.Filter = _      "Text Files|*.txt|All Files|*.*"   SaveFileDialog1.FilterIndex = 0   If SaveFileDialog1.ShowDialog = DialogResult.OK Then       Dim FS As FileStream = SaveFileDialog1.OpenFile       Dim SW As New StreamWriter(FS)       SW.Write(TextBox1.Text)       SW.Close()       FS.Close()   End If 同樣採用類似的語句,我們讀取一個文字檔,並把內容顯示在TextBox控制項中。StreamReader的ReadToEnd方法返迴文件的全部內容。    OpenFileDialog1.Filter = _      "Text Files|*.txt|All Files|*.*"   OpenFileDialog1.FilterIndex = 0   If OpenFileDialog1.ShowDialog = DialogResult.OK Then       Dim FS As FileStream       FS = OpenFileDialog1.OpenFile       Dim SR As New StreamReader(FS)       TextBox1.Text = SR.ReadToEnd       SR.Close()       FS.Close()   End If Persisting Collections

 集合的儲存


大多數程式處理對象集合而不是單個的對象。對於集合資料,首先建立一個數組(或者是其他類型的集合,比如ArrayList或HashTable),用對象填充,然後一個Serialize方法就可以序列化真箇集合,是不是很簡單?下面的例子,首先建立一個有兩個Person對象的ArrayList,然後序列化本身:
 
   Dim FS As New System.IO.FileStream _
      ("c:/test.txt", IO.FileMode.Create)
   Dim BinFormatter As New Binary.BinaryFormatter()
   Dim P As New Person()
   Dim Persons As New ArrayList
   P = New Person()
   P.Name = "Person 1"
   P.Age = 35
   P.Income = 32000
   Persons.Add(P)
  
   P = New Person()
   P.Name = "Person 2"
   P.Age = 50
   P.Income = 72000
   Persons.Add(P)
  
   BinFormatter.Serialize(FS, Persons)
以儲存序列化資料的檔案為參數,調用一個BinaryFormatter執行個體的Deserialize方法,就會返回一個對象,然後把它轉化為合適的類型。下面的代碼還原序列化檔案中的所有對象,然後處理所有的Person對象:
 
   FS = New System.IO.FileStream _
      ("c:/test.txt", IO.FileMode.OpenOrCreate)
   Dim obj As Object
   Dim P As Person(), R As Rectangle()
   Do
       obj = BinFormatter.Deserialize(FS)
       If obj.GetType Is GetType(Person) Then
           P = CType(obj, Person)
           ' Process the P objext
       End If
   Loop While FS.Position < FS.Length - 1
   FS.Close()
下面的例子調用Deserialize方法還原序列化真箇集合,然後把傳回值轉換為合適的類型(Person):
   FS = New System.IO.FileStream("c:/test.txt", IO.FileMode.OpenOrCreate)
   Dim obj As Object
   Dim Persons As New ArrayList
   obj = CType(BinFormatter.Deserialize(FS), ArrayList)
   FS.Close()

各種對象的儲存

採用BinaryFormatte以二進位的形式,或者用SoapFormatter類以XML格式都可以序列化一個具體的對象。只要把所有BinaryFormatter的引用改為SoapFormatter,無需改變任何代碼,就可以以XML格式序列化對象。
首先建立一個BinaryFormatter執行個體:
   Dim BinFormatter As New Binary.BinaryFormatter()
然後建立一個用於儲存序列化對象的FileStream對象:
   Dim FS As New System.IO.FileStream("c:/test.txt", IO.FileMode.Create)

接著調用BinFormatter的Serialize方法序列化任何可以序列化的framework對象:
   R = New Rectangle(rnd.Next(0, 100),rnd.Next(0, 300), _
 rnd.Next(10, 40),rnd.Next(1, 9))
   BinFormatter.Serialize(FS, R)

加一個Serializable屬性使得自訂的對象可以序列化
 
   <Serializable()> Public Structure Person
       Dim Name As String
       Dim Age As Integer
       Dim Income As Decimal
   End Structure

下面代碼建立一個Person對象執行個體,然後調用BinFormatter的Serialize方法序列化自訂對象:
   P = New Person()
   P.Name = "Joe Doe"
   P.Age = 35
   P.Income = 28500
   BinFormatter.Serialize(FS, P)
你也可以在同一個Stream中接著序列化其他對象,然後以同樣的順序讀回。例如,在序列化Person對象之後接著序列化一個Rectangle對象:
   BinFormatter.Serialize(FS, New Rectangle(0, 0, 100, 200))
 建立一個BinaryFormatter對象,調用其Deserialize方法,然後把返回的值轉化為正確的類型,就是整個還原序列化過程。然後接著發序列化Stream的其他對象。
假定已經序列化了Person和Rectangle兩個對象,以同樣的順序,我們還原序列化就可以得到原來的對象:

 
   Dim P As New Person()
   P = BinFormatter.Serialize(FS, Person)
   Dim R As New Rectangle
   R = BinFormatter.Serialize(FS, Rectangle)

 

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.