StreamReader和StreamWriter類用于從文本文件讀取和寫入數據。 這些類繼承自抽象基類Stream,它支持將字節(jié)讀取和寫入文件流。
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. 從輸入流中讀取下一個字符,并將字符位置前移一個字符。 |
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
猜測當你編譯和運行程序時顯示什么?S.N | 方法名稱和用途 |
---|---|
1 | Public 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. 將行終止符寫入文本字符串或流。 |
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
Zara Ali
Nuha Ali
Amir Sohel
M Amlan
更多建議: