vb 文本文件操作
发布网友
发布时间:2022-04-22 14:54
我来回答
共3个回答
热心网友
时间:2023-10-17 15:43
1、把文本文件内容写到TextBox:
Dim TempFile As Long
Dim LoadBytes() As Byte
TempFile=FreeFile
Open 文件名 For Binary As #TempFile
Redim LoadBytes(1 To Lof(TempFile)) As Byte
Get #TempFile,,LoadBytes
Close TempFile
Text1.Text=StrConv(LoadBytes,vbUniCode)
2、把TextBox内容写入文本文件:
Dim TempFile As Long
Dim SaveBytes() As Byte
SaveBytes=StrConv(Text1.Text,vbFromUniCode)
TempFile=FreeFile
Open 文件名 For Binary As #TempFile
Put #TempFile,,SaveBytes
Close TempFile
3、删除TXT文件里的内容:
Private Function DelLine(strFile As String, RLine As Long, newFile As String, SameLine As Boolean)
Dim s As String, n As String, i As Long
i = 1
'//打开源文件
Open strFile For Input As #1
Do Until EOF(1)
Line Input #1, s
If RLine = i Then '如果是指定的行数就进行下面的操作
If SameLine = True Then '是否保持源文件行数不变(以空白字符替换这一行内容)的提示,True保持源文件的行数,False为直接删除这一行的内容
s = ""
n = n & s & vbCrLf '将空字符串赋给变量n,以保持源文件的行数
' MsgBox strFile & " 文件中,第 " & RLine & " 行内容" & vbCrLf & s & vbCrLf & "已经删除", vbInformation, "消息提示"
End If
' s="也可以把这一行的内容改成自己需要的"
Else '如果不是指定的行数,就将s的内容赋给变量n 以存储数据
n = n & s & vbCrLf '将s的内容赋给n 并以一个回车符号结束....
End If
i = i + 1
Loop
Close #1
'//写入新文件,如果和源文件同名则会覆盖源文件
Open newFile For Output As #2
Print #2, n '将n变量里的数据写入新文件
Close #2
End Function
'调用方法:
'比如要把c:\1.txt 删除其中的第5行内容,并保留源文件总行数(删除的这行插入一空字符串)
'DelLine "C:\1.txt", 5, "C:\2.txt", True
'删除C:\1.txt 删除里面的第一行,且不保留文件的总行数
DelLine "C:\1.txt", 1, "C:\1.txt", False
热心网友
时间:2023-10-17 15:43
Private Sub WriteTxt(StrFile As String, LngLine As Long, StrR As String)
'参数一 要写入的文件地址,参数二 修改的行数 ,参数三 写入或替换的字符
Dim StrFile As String, StrOut As String
Dim x As Long
If Dir(StrFile) <> "" Then
Open StrFile For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
x = x + 1
If x = LngLine Then tmp = StrR '
StrOut = StrOut & tmp & vbCrLf
Loop
Close #1
Else
StrOut = StrR
End If
Open StrFile For Output As #1
Print #1, StrOut
Close #1
End Sub
Private Sub ReadTxt(MyText As TextBox, StrFile As String, LngLine As Long)
'参数一 要输出的文本框控件,参数二 文件地址,参数三 读取的行数
Dim StrFile As String, StrOut As String
Dim x As Long
If Dir(StrFile) <> "" Then
Open StrFile For Input As #1
Do While Not EOF(1)
Line Input #1, tmp
x = x + 1
If x = LngLine Then MyText.Text = tmp: Exit Do
Loop
Close #1
End If
End Sub
Private Sub Command1_Click()
Dim a As String
a = Chr(34) & "yes" & Chr(34) & "," & Chr(34) & "123" & Chr(34) & "," & Chr(34) & "qwe" & Chr(34)
Call WriteTxt("z:\1.txt", 1, a)
End Sub
Private Sub Command2_Click()
Call ReadTxt(Text1, "z:\1.txt", 1)
End Sub
Private Sub Command3_Click()
Dim a As String
a = Chr(34) & "yun" & Chr(34) & "," & Chr(34) & "e:\123.exe" & Chr(34) & "," & Chr(34) & "1" & Chr(34)
Call WriteTxt("z:\1.txt", 2, a)
End Sub
Private Sub Command4_Click()
Call ReadTxt(text2, "z:\1.txt", 2)
End Sub
热心网友
时间:2023-10-17 15:44
Private Sub Command1_Click()
Open "hehe.txt" For Output As #1
Write #1, "yes", "123", "qwe"
Close #1
End Sub
Private Sub Command2_Click()
Open "hehe.txt" For Input As #1
Dim A, B, C As String
Input #1, A, B, C
Form1.Text1 = A & B & C
Close #1
End Sub
Private Sub Command3_Click()
Open "hehe.txt" For Append As #1
Write #1, "yun", "e:\123.exe", "1"
Close #1
End Sub
Private Sub Command4_Click()
Open "hehe.txt" For Input As #1
Dim A, B, C As String
Input #1, A, B, C
Form1.Text2 = A & B & C
Close #1
End Sub