VB.NET文件對象基礎教學指南
VB.NET是一款由微軟公司推出的基于.netframeworkSDK的一種語言。它的應用機制和Java相同,能實現代碼托管,具有強大的面向對象特性。下面我們可以通過對VB.NET文件對象的相關介紹來對這一語言進行一個詳細的解讀。
VB.NET文件對象代表了一個文件,而且有一些非常有用的方法來檢驗一個文件的存在以及重命名和刪除一個文件。例如:
- Dim fl as File
- fl=new File("foo.txt")
- if(fl.Exists) 'if the file exists
- fl.Delete 'delete it
- End If
用戶也可以使用File對象來獲取一個FileStream對象,然后通過使用FileStream對象來讀寫文件數據:
- Dim ts as TextStream
- Dim fs as FileStream
- ts=fl.OpenText 'open a text file for reading
- fs=fl.OpenRead 'open any file for reading
(1)VB.NET文件對象讀取一個文本文件(TextFile)
了讀取一個文本文件,用戶可以使用File對象去獲取一個StreamReader對象,然后使用文本流(text stream)的讀取方法:
- Dim ts as StreamReader
- ts=fl.OpenText()
- s=ts.readLine
(2)VB.NET文件對象寫一個文本文件(Write a Text File)
要建立和寫一個文本文件,用戶可以用CreateText方法得到一個StreamWrite對象,然后進行操作,比如:
- Dim sw as streamWriter
- sw=fl.CreateText
- sw.writeLine("write text into file")
如果用戶想對一個已經存在的文件進行操作,可以使用一個帶有邏輯參數的對象來進行操作:
- sw=new StreamWriter(path,true)
VB.NET文件對象的相關應用方法就為大家介紹到這里。
【編輯推薦】

















