如何让VB6读写文件更快

来源:百度知道 编辑:UC知道 时间:2024/06/01 12:19:29
读/写一个几十KB的文件,用For…Next语句重复循环Put/Get语句,需要十几秒钟!

可以瞬间完成啊!
用二进制方式打开文件一次性存取 65K 数据,时间不到 1 秒。

'存入
Private Sub Command1_Click()
Dim nStr As String, H As Long, B() As Byte, F As String

'先用For…Next语句将要写入的数据合成到 nStr,进行一次性存入。下面是我的65K例子
nStr = "我的123" & String(65535, "A") & "例子" '-------- 65K 的数据

F = "C:\aaa.txt"
H = FreeFile
Open F For Binary As #H '用二进制方式打开一个文件
Put #H, , nStr
Close #H
End Sub

'读出
Private Sub Command2_Click()
Dim nStr As String, H As Long, B() As Byte, F As String, S As Long
F = "C:\aaa.txt"
S = FileLen(F)
ReDim B(1 To S)
H = FreeFile
Open F For Binary As #H '用二进制方式打开一个文件
Get #H, , B
Close #H
nStr = StrConv(B, vbUnicode) '字符串转变为 vbUnicode 字符
End Sub