我正在尝试将文本文件加载到数组中。在vb

时间:2014-12-03 23:27:40

标签: vb.net file-io

这是我到目前为止所拥有的

数组大小为(199,2)

该文件包含如下所示的行:Canada,34026478,12938494                                           USA,3377786,5345678

Sub LoadFromFile(filename As String)

    Dim Reader As StreamReader = File.OpenText(filename)
    Dim Currentline As String
    Dim lineArray() As String

    Do While Reader.EndOfStream = False
        Currentline = Reader.ReadLine
        lineArray = Currentline.Split(","c)
    Loop

    Using sr As StreamReader = New StreamReader(filename)
        Do While sr.Peek() >= 0
            For Each c As Char In sr.ReadLine.Split
                Try
                    ValuesFromFile(y, x) = CStr(Integer.Parse(c))
                Catch ex As Exception 'i assume this is the only possible error, but we could be out of bounds due to assuming the actual size of the file/line... catch specific exceptions as necessary'
                End Try
                y += 1
            Next
            x += 1
            y = 0
        Loop
    End Using



    Reader.Close()
End Sub

0 个答案:

没有答案