国产gaysexchina男同gay,japanrcep老熟妇乱子伦视频,吃奶呻吟打开双腿做受动态图,成人色网站,国产av一区二区三区最新精品

VB.Net - 從文本文件讀取和寫入

2018-12-16 18:06 更新

StreamReader和StreamWriter類用于從文本文件讀取和寫入數據。 這些類繼承自抽象基類Stream,它支持將字節(jié)讀取和寫入文件流。


StreamReader類

StreamReader類還繼承自抽象基類TextReader,它代表用于閱讀一系列字符的讀者。 下表描述了StreamReader類的一些常用方法:


S.N方法名稱和用途
1

Public Overrides Sub Close

It closes the StreamReader object and the underlying stream and releases any system resources associated with the reader.

它關閉StreamReader對象和基礎流,并釋放與讀取器相關聯(lián)的任何系統(tǒng)資源。

2

Public Overrides Function Peek As Integer

Returns the next available character but does not consume it.

返回下一個可用字符,但不使用它。

3

Public Overrides Function Read As Integer

Reads the next character from the input stream and advances the character position by one character.

從輸入流中讀取下一個字符,并將字符位置前移一個字符。


示例:

以下示例演示讀取名為Jamaica.txt的文本文件。 該文件讀為:
Down the way where the nights are gay
And the sun shines daily on the mountain top
I took a trip on a sailing ship
And when I reached Jamaica
I made a stop
Imports System.IO
Module fileProg
   Sub Main()
      Try
          ' Create an instance of StreamReader to read from a file. 
          ' The using statement also closes the StreamReader. 
          Using sr As StreamReader = New StreamReader("e:/jamaica.txt")
              Dim line As String
              ' Read and display lines from the file until the end of  
              ' the file is reached. 
              line = sr.ReadLine()
              While (line <> Nothing)
                  Console.WriteLine(line)
                  line = sr.ReadLine()
              End While
          End Using
      Catch e As Exception
          ' Let the user know what went wrong.
          Console.WriteLine("The file could not be read:")
          Console.WriteLine(e.Message)
      End Try
      Console.ReadKey()
   End Sub
End Module
猜測當你編譯和運行程序時顯示什么?

StreamWriter類

StreamWriter類繼承自抽象類TextWriter,表示一個寫入器,它可以寫一系列字符。

下表顯示了此類最常用的一些方法:
S.N方法名稱和用途
1Public Overrides Sub Close 
Closes the current StreamWriter object and the underlying stream.
關閉當前StreamWriter對象和基礎流。
2

Public Overrides Sub Flush

Clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

清除當前寫入程序的所有緩沖區(qū),并使任何緩沖的數據寫入基礎流。

3

Public Overridable Sub Write (value As Boolean)

Writes the text representation of a Boolean value to the text string or stream. (Inherited from TextWriter.)

將布爾值的文本表示寫入文本字符串或流。(從TextWriter繼承。)

4

Public Overrides Sub Write (value As Char)

Writes a character to the stream.

將字符寫入流。

5

Public Overridable Sub Write (value As Decimal)

Writes the text representation of a decimal value to the text string or stream.

將十進制值的文本表示寫入文本字符串或流。

6

Public Overridable Sub Write (value As Double)

Writes the text representation of an 8-byte floating-point value to the text string or stream.

將8字節(jié)浮點值的文本表示寫入文本字符串或流。

7

Public Overridable Sub Write (value As Integer)

Writes the text representation of a 4-byte signed integer to the text string or stream.

將4字節(jié)有符號整數的文本表示寫入文本字符串或流。

8

Public Overrides Sub Write (value As String)

Writes a string to the stream.

將字符串寫入流。

9

Public Overridable Sub WriteLine

Writes a line terminator to the text string or stream.

將行終止符寫入文本字符串或流。

以上列表并不詳盡。 有關方法的完整列表,請訪問Microsoft的文檔。

示例:

以下示例演示使用StreamWriter類將文本數據寫入文件:
Imports System.IO
Module fileProg
   Sub Main()
      Dim names As String() = New String() {"Zara Ali", _
         "Nuha Ali", "Amir Sohel", "M Amlan"}
      Dim s As String
      Using sw As StreamWriter = New StreamWriter("names.txt")
          For Each s In names
              sw.WriteLine(s)
          Next s
      End Using
      ' Read and show each line from the file. 
      Dim line As String
      Using sr As StreamReader = New StreamReader("names.txt")
          line = sr.ReadLine()
          While (line <> Nothing)
              Console.WriteLine(line)
              line = sr.ReadLine()
          End While
      End Using
      Console.ReadKey()
   End Sub
End Module

當上述代碼被編譯和執(zhí)行時,它產生以下結果:
Zara Ali
Nuha Ali
Amir Sohel
M Amlan 

以上內容是否對您有幫助:
在線筆記
App下載
App下載

掃描二維碼

下載編程獅App

公眾號
微信公眾號

編程獅公眾號