1. 程式人生 > >vb6.0 快速讀取txt

vb6.0 快速讀取txt

Function GetFile(FileName As String) As String
Dim i As Integer, s As String, BB() As Byte
If Dir(FileName) = "" Then Exit Function
i = FreeFile
ReDim BB(FileLen(FileName) - 1)
Open FileName For Binary As #i
Get #i, , BB
Close #i
s = StrConv(BB, vbUnicode)
GetFile = s
End Function

'呼叫舉例:
dim s as string
s=GetFile("c:\1.txt")
如果檔案中沒有中文字元,那麼下面程式碼讀整個檔案的速度是非常快的,我讀了一個6M的檔案,只用了不到1秒。如果有中文字元,就需要把s = Input$(LOF(1), #1)中的LOF(1)減去中文字元數量。
Dim r As String, s As String
r = Environ("userprofile")
r = r + "\桌面\9898.txt"'要讀的檔案路徑,在桌面上的9898.txt
Open r For Input As #1
s = Input$(LOF(1), #1)
Debug.Print s
Close #1