如何讀取和寫入至文字檔案,使用 Visual Basic 2005年或 Visual Basic.NET
--
'Read a Text FileImports System.IO
Module Module1
Sub Main()
Dim objStreamReader As StreamReader
Dim strLine As String
'Pass the file path and the file name to the StreamReader constructor.
objStreamReader = New StreamReader("C:\Boot.ini")
'Read the first line of text.
strLine = objStreamReader.ReadLine
'Continue to read until you reach the end of the file.
Do While Not strLine Is Nothing
'Write the line to the Console window.
Console.WriteLine(strLine)
'Read the next line.
strLine = objStreamReader.ReadLine
Loop
'Close the file.
objStreamReader.Close()
Console.ReadLine()
End Sub
End Module
'Write a Text File: Version 1
Imports System.IO
Module Module1
Sub Main()
Dim objStreamWriter As StreamWriter
'Pass the file path and the file name to the StreamWriter constructor.
objStreamWriter = New StreamWriter("C:\Text.txt")
'Write a line of text.
objStreamWriter.WriteLine("Hello World")
'Write a second line of text.
objStreamWriter.WriteLine("From the StreamWriter class")
'Close the file.
objStreamWriter.Close()
End Sub
End Module
'Write a Text File: Version 2
Imports System.IO
Imports System.Text
Module Module1
Sub Main()
Dim objStreamWriter As StreamWriter
Dim x As Long
'Open the file.
objStreamWriter = New StreamWriter("C:\Test2.txt", True, _
Encoding.Unicode)
'Write out the numbers 1 through 10 on the same line.
For x = 1 To 10
objStreamWriter.Write(x)
Next x
'Close the file.
objStreamWriter.Close()
End Sub
End Module
沒有留言:
張貼留言