VB.net如何读取文本文件中的部分数据

weeweew 2008-08-05 06:16:43
我现在要读取一个文本文件,文件结构如下:
variable x,y,z,u,v,w
zone I=398 J=1 K=0
300 200 0 100 200 0
300 100 0 100 200 0
...............
...............
zone I=398 J=256 k=0....
300 200 0 100 200 3000
................
现在首先需要查找每个标志行 得到 I J U V 等的数据,付给变量(I和J表示数据的行数和列数),然后根据I和J的值用for循环读取下面的数据。请问用vb.net如何实现?
还请高手帮忙,在此先谢过了。
...全文
389 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
只在此山中 2008-08-06
  • 打赏
  • 举报
回复
'自己加数组变量存储需要的数据
Dim reader As System.IO.StreamReader=New System.IO.StreamReader("c:\aa.txt", System.Text.Encoding.Default)
'Dim reader As StreamReader = File.OpenText("c:\myfile.txt")
Dim line As String = reader.ReadLine()
While Not line Is Nothing
line = reader.ReadLine()
'自己处理读到的一行
End While
'读完毕
reader.Close()
破咖啡罐子 2008-08-06
  • 打赏
  • 举报
回复

Dim aryrst As New ArrayList
Dim CurrentPosition As Integer = 0
For Each strline As String In IO.File.ReadAllLines("YourFilePathAndName") '循环每行
Dim strI As String = ""
Dim strJ As String = ""
Dim strK As String = ""
If strline.StartsWith("variable") Then Continue For
If strline.StartsWith("zone") Then
Dim strZone() As String = strline.Split(CChar(" "))
strI = strZone(1).Replace("I=", "")
strJ = strZone(1).Replace("J=", "")
strK = strZone(1).Replace("K=", "")
CurrentPosition = 0
End If
If CurrentPosition.ToString = strI Then
aryrst.Add(strline.Split(CChar(" "))(CInt(strJ)))
End If
CurrentPosition += 1
Next


1.没测试所以不保证代码正确性,只是提供一个思路,点到即止。
2.“I和J表示数据的行数和列数”,你没说索引是从0开始还是1,所以我以0开始,我的习惯。
weeweew 2008-08-06
  • 打赏
  • 举报
回复
多谢了

16,717

社区成员

发帖
与我相关
我的任务
社区描述
VB技术相关讨论,主要为经典vb,即VB6.0
社区管理员
  • VB.NET
  • 水哥阿乐
  • 无·法
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧