边读边计算的如果用计数器则可以指定读取第几行内容第一例(答复:咨询VB读文本文件,文本文件有N行,3列。想得到1、2列相同时。3列的值求和)http://bbs.csdn.net/topics/390919855#
Dim 文件路径 As String = System.Environment.CurrentDirectory + "\新建文本文档.txt" Dim 内容 As New List(Of String),暂存 As String = ",1",计算 As String() If New FileInfo(文件路径).Exists Then Dim 打开 As New FileStream(文件路径,FileMode.Open) Dim 读取 As New StreamReader(打开) Dim 判断 = 0 While (读取.EndOfStream <> True) Dim 取值 = 读取.ReadLine() 判断 += 1 If (暂存.Split(",")(0) <> 取值.Split(",")(0)) Or (暂存.Split(",")(1) <> 取值.Split(",")(1)) Then If 判断 > 1 Then 内容.Add(暂存) End If 暂存 = 取值 Else 计算 = 取值.Split(",") 计算(2) = (Int16.Parse(计算(2)) + Int16.Parse(暂存.Split(",")(2))).ToString 内容.Add(String.Join(",",计算)) 判断 = 0 End If End While End If