VB.NET中 怎么读入TXT文本的数字到一个数组。。

2024-11-25 12:57:44
推荐回答(2个)
回答1:

代码及注释如下:

Dim aa$, s, ss'定义变量
Private Sub Form_Load()
Open "c:\temp.txt" For Input As #1'打开txt文件
aa = StrConv(InputB(LOF(1), 1), vbUnicode)'将文本读入aa中
Close #1'关闭文件
End Sub

Private Sub Command1_Click()
s = Split(aa, vbNewLine)'将文本分离出来并放入数组s中
For i = 0 To UBound(s)'i从0到S的最大元素数循环
If s(i) <> "" Then'如果数组部位空
ss = Split(s(i), "=")'择将数组按照=分离,并放入数组ss
Print Val(ss(1))‘显示数值化的数组ss第一个元素
End If
Next i
End Sub

回答2:

参考一下这个:
VB2008代码,用了一个对话框打开指定的文本文件,一个列表框显示读入的结果。

Dim myStr() As String
Dim a() As Integer
Dim i As Integer
Dim sr As String

Me.OpenFileDialog1.ShowDialog()
Dim FileName As String = Me.OpenFileDialog1.FileName
sr = IO.File.ReadAllText(FileName)
myStr = Split(sr, " ")
Debug.Print(myStr.GetUpperBound(0))

ReDim a(myStr.GetUpperBound(0))
For i = 0 To myStr.GetUpperBound(0)
a(i) = Integer.Parse(myStr(i).Trim)
Me.ListBox1.Items.Add("a(" & i & ")= " & a(i))
Next