VB.NET打開二進制文件用fileopen完成,打開二進制文件的形式為:openmode.binary
在蘿北等地區(qū),都構建了全面的區(qū)域性戰(zhàn)略布局,加強發(fā)展的系統(tǒng)性、市場前瞻性、產品創(chuàng)新能力,以專注、極致的服務理念,為客戶提供網站建設、網站制作 網站設計制作按需定制設計,公司網站建設,企業(yè)網站建設,品牌網站設計,成都全網營銷,成都外貿網站建設公司,蘿北網站建設費用合理。
讀取二進制文件用的是fileget方法,寫入二進制文件用的是fileput方法。
應用示例:將一批隨機數(shù)保存在一個dat文件中,然后再將其提取到文本框中。
二進制文件的讀寫一批隨機數(shù)的存取,程序為:
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
大概思路:
1,一次性讀出所有內容到一個字符串變量str
2,str.Trim去掉前后空白字符
3, Dim str_row() As String = str.Trim.Split(vbCrLf)'行數(shù)據
4, Dim str_col() As String = str_row(0).Split(vbTab)'分離出第1行的列數(shù)據---如果是第2行改0為1
5,str_col(0)就是第一列數(shù)據,行對應第4步中的行
寫入:Dim sr As New IO.StreamWriter(Application.StartupPath "/寫入的文本.txt")
sr.WriteLine("寫入的內容") sr.Close()讀?。篒f (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
1、實現(xiàn)上傳按鈕方法代碼。
2、判斷圖片對象是否為空代碼。
3、取得數(shù)據庫字段 dt.Rows(0)("Pic")方法代碼。
4、字節(jié)數(shù)組轉換為Image類型方法代碼。
5、處理SQL中操作Image類型方法代碼。
6、實現(xiàn)的上傳結果。
1、新建一個標準的VB EXE工程,只有一個Form,F(xiàn)orm上有兩個按鈕: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運行完成,就完成了讀取文本文件內容并輸出到立即窗口。
imports System.IO
讀取指定文件
'
'讀取指定文本文件
Public Function readtext(ByVal path As String)
If path = "" Then
readtext = "操作失??!"
Exit Function
End If
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, FileMode.Open)
Dim sr As New StreamReader(fs)
Dim str As String
str = sr.ReadToEnd.ToString
sr.Close()
fs.Close()
readtext = str
Else
readtext = "操作失??!"
End If
Catch ex As Exception
readtext = "操作失??!"
End Try
End Function
'向指定文件寫入數(shù)據
Public Function writetext(ByVal path As String, ByVal opi As Integer, ByVal msg As String)
If path = "" Then
writetext = "操作失??!"
Exit Function
End If
Dim op As FileMode
Select Case opi
Case 1
op = FileMode.Append
Case 2
op = FileMode.Create
Case Else
op = FileMode.Create
End Select
Try
If File.Exists(path) = True Then
Dim fs As New FileStream(path, op)
Dim sr As New StreamWriter(fs)
sr.WriteLine(msg)
sr.Close()
fs.Close()
writetext = "操作完成!"
Else
writetext = "操作失??!"
End If
Catch ex As Exception
writetext = "操作失??!"
End Try
End Function