vb.net的檔案對象(file object)

來源:互聯網
上載者:User

檔案對象代表了一個檔案,而且有一些非常有用的方法來檢驗一個檔案的存在以及重新命名和刪除一個檔案。例如:
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)讀取一個文字檔(TextFile)
為了讀取一個文字檔,使用者可以使用File對象去擷取一個StreamReader對象,然後使用文字資料流(text stream)的讀取方法:
Dim ts as StreamReader
ts=fl.OpenText()
s=ts.readLine
(2)寫一個文字檔(Write a Text File)
要建立和寫一個文字檔,使用者可以用CreateText方法得到一個StreamWrite對象,然後進行操作,比如:
Dim sw as streamWriter
sw=fl.CreateText
sw.writeLine("write text into file")
如果使用者想對一個已經存在的檔案進行操作,可以使用一個帶有邏輯參數的對象來進行操作:
sw=new StreamWriter(path,true)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.