VB.NET打開二進制文件用fileopen完成,打開二進制文件的形式為:openmode.binary
專注于為中小企業提供成都網站設計、成都網站建設服務,電腦端+手機端+微信端的三站合一,更高效的管理,為中小企業資陽免費做網站提供優質的服務。我們立足成都,凝聚了一批互聯網行業人才,有力地推動了上1000家企業的穩健成長,幫助中小企業通過網站建設實現規模擴充和轉變。
讀取二進制文件用的是fileget方法,寫入二進制文件用的是fileput方法。
應用示例:將一批隨機數保存在一個dat文件中,然后再將其提取到文本框中。
二進制文件的讀寫一批隨機數的存取,程序為:
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim x, i, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
For i = 1 To 8
x = Int(Rnd() * 100)
s = s + Str(x)
FilePut(fn, x)
Next
FileClose(fn)
TextBox1.Text = s
End Sub
Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click
Dim x, fn As Integer
Dim s As String = ""
fn = FreeFile()
FileOpen(fn, "d:\data.dat", OpenMode.Binary)
Do While Not EOF(fn)
FileGet(fn, x)
s = s + Str(x) + " "
Loop
FileClose(fn)
TextBox1.Text = s
End Sub
寫入:Dim sr As New IO.StreamWriter(Application.StartupPath "/寫入的文本.txt")
sr.WriteLine("寫入的內容") sr.Close()讀取:If (File.Exists(Application.StartupPath "/msg.txt")) Then
Dim fm As New IO.FileStream(Application.StartupPath "/讀取的文本.txt", FileMode.Open)
Dim sr As IO.StreamReader = New IO.StreamReader(fm)
Do While sr.Peek() = 0
TextBox1.Text = sr.ReadLine() (讀取文本到文本框)
Loop end if
dim filename as string = "文件名" Using myfilestream As New FileStream(FileName, FileMode.Open, FileAccess.Read)
Dim data() As Byte
ReDim data(myfilestream.Length - 1)
myfilestream.Read(data, 0, myfilestream.Length)
myfilestream.Close()
' data是你要的結果,為byte(), End Using
1、新建一個標準的VB EXE工程,只有一個Form,Form上有兩個按鈕:Command1和Command2。
2、雙擊Command1添加如下代碼
Private Sub Command1_Click()
Dim strFile? ? ?As String
Dim intFile? ? ?As Integer
Dim strData? ? ?As String
strFile = "c:\學生成績.txt"
intFile = FreeFile
Open strFile For Input As intFile
strData = StrConv(InputB(FileLen(strFile), intFile), vbUnicode)
Debug.Print strData
Close intFile
End Sub
3、按F8開始單步調試代碼,點擊Command1,進入單步調試功能,
4、多次按下F8或直接按下F5運行完成,就完成了讀取文本文件內容并輸出到立即窗口。
文章題目:vb.net讀取文件流,vbnet讀取文件內容
當前地址:http://m.kartarina.com/article42/hddjhc.html
成都網站建設公司_創新互聯,為您提供網站導航、服務器托管、建站公司、自適應網站、網站制作、商城網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯