亚洲欧洲国产欧美一区精品,激情五月亚洲色五月,最新精品国偷自产在线婷婷,欧美婷婷丁香五月天社区

      考試首頁(yè) | 考試用書 | 培訓(xùn)課程 | 模擬考場(chǎng) | 考試論壇  
        當(dāng)前位置:編程開發(fā) > DotNET > VB.Net > 文章內(nèi)容
        

      vb.net讀寫文件代碼

       [ 2017年7月5日 ] 【

        讀取和寫入文件

        以下示例將一行文本寫入文件。

        ' Write text to a file

        Sub WriteTextToFile()

        Dim file As New System.IO.StreamWriter("c:test.txt")

        file.WriteLine("Here is the first line.")

        file.Close()

        End Sub

        以下示例將文件中的文本讀取到一個(gè)字符串變量中,然后將該文本寫到控制臺(tái)。

        Sub ReadTextFromFile()

        Dim file As New System.IO.StreamReader("c:test.txt")

        Dim words As String = file.ReadToEnd()

        Console.WriteLine(words)

        file.Close()

        End Sub

        以下示例在現(xiàn)有文件中添加文本。

        Sub AppendTextToFile()

        Dim file As New System.IO.StreamWriter("c:test.txt", True)

        file.WriteLine("Here is another line.")

        file.Close()

        End Sub

        以下示例一次從文件中讀取一行,然后將每行文本打印到控制臺(tái)。

        Sub ReadTextLinesFromFile()

        Dim file As New System.IO.StreamReader("c:test.txt")

        Dim oneLine As String

        oneLine = file.ReadLine()

        While (oneLine <> "")

        Console.WriteLine(oneLine)

        oneLine = file.ReadLine()

        End While

        file.Close()

        End Sub

        文件編碼

        默認(rèn)情況下,StreamReader 和 StreamWriter 類都使用 UTF-8 編碼。UTF-8 編碼正確處理 Unicode 字符并確保操作系統(tǒng)的本地化版本之間保持一致。

        可使用 StreamReader 自動(dòng)檢測(cè)文件的編碼,或者將文件的編碼指定為構(gòu)造函數(shù)上的參數(shù)。

        StreamWriter 在其構(gòu)造函數(shù)上采用一個(gè)編碼參數(shù)。如果指定編碼,則 Visual Basic 寫入文件以指示所使用的編碼。

      將考試網(wǎng)添加到收藏夾 | 每次上網(wǎng)自動(dòng)訪問考試網(wǎng) | 復(fù)制本頁(yè)地址,傳給QQ/MSN上的好友 | 申請(qǐng)鏈接 | 意見留言 TOP
      關(guān)于本站  網(wǎng)站聲明  廣告服務(wù)  聯(lián)系方式  站內(nèi)導(dǎo)航  考試論壇
      Copyright © 2007-2013 中華考試網(wǎng)(Examw.com) All Rights Reserved