如何加快读取文本文件(或者csv文件)的速度,谢谢啦
老板嫌速度慢,叫我加快读取速度,请各位帮帮忙吧
代码如下,读的是csv文件
Dim sr As System.IO.TextReader
Dim array_line() As String
Dim i As Integer
Dim j As Integer
Dim strLine As String
Try
PsGetData = True
sr = New StreamReader(strPath, System.Text.Encoding.Default)
sr.ReadLine()
'计算行数
i = 1
Do
If sr.Peek() = -1 Then
Exit Do
End If
strLine = sr.ReadLine()
i = i + 1
Loop
sr.Close()
'定义数组
ReDim array(i, int項目数)
'取得数据
sr = New StreamReader(strPath, System.Text.Encoding.Default)
i = 1
Do
array_line = Split(sr.ReadLine(), ",")
For j = 0 To UBound(array_line)
array(i, j + 1) = array_line(j)
Next
If sr.Peek = -1 Then
Exit Do
End If
i = i + 1
Loop
Catch ex As DirectoryNotFoundException
Call cf_MsgScreen(Me, C_ERROR, C_DIRECTORYNOTFOUND)
PsGetData = False
Exit Function
Catch ex As FileNotFoundException
Call cf_MsgScreen(Me, C_ERROR, C_FILENOTFOUND)
PsGetData = False
Exit Function
Catch ex As Exception
Call cf_MsgScreen(Me, C_ERROR, ex.Message)
PsGetData = False
Exit Function
End Try
sr.Close()
PsGetData = True
End Function
代码中,先计算了行数,用来定义数组,然后将数据读进数组,是不适因为使用StreamReader,所以慢呢。