首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > .NET > VB Dotnet >

vb.net读取txt文档的有关问题

2012-08-15 
vb.net读取txt文档的问题NODEXYZTHXYTHYZTHZX115956.12152.0.00000.000.000.00216015.12152.0.00000.000.0

vb.net读取txt文档的问题
NODE X Y Z THXY THYZ THZX
  1 15956. 12152. 0.0000 0.00 0.00 0.00
  2 16015. 12152. 0.0000 0.00 0.00 0.00
  3 15976. 12152. 0.0000 0.00 0.00 0.00
  4 15996. 12152. 0.0000 0.00 0.00 0.00
  5 16015. 12212. 0.0000 0.00 0.00 0.00
  6 16015. 12172. 0.0000 0.00 0.00 0.00
  7 16015. 12192. 0.0000 0.00 0.00 0.00
  8 15956. 12212. 0.0000 0.00 0.00 0.00
  9 15996. 12212. 0.0000 0.00 0.00 0.00
  10 15976. 12212. 0.0000 0.00 0.00 0.00
  11 15956. 12192. 0.0000 0.00 0.00 0.00
  12 15956. 12172. 0.0000 0.00 0.00 0.00
  13 15977. 12186. 0.0000 0.00 0.00 0.00
  14 15983. 12202. 0.0000 0.00 0.00 0.00
  15 15997. 12179. 0.0000 0.00 0.00 0.00
  16 15980. 12165. 0.0000 0.00 0.00 0.00
  17 15970. 12201. 0.0000 0.00 0.00 0.00
  18 15997. 12197. 0.0000 0.00 0.00 0.00
  19 16001. 12164. 0.0000 0.00 0.00 0.00
  20 16015. 12152. 1682.9 0.00 0.00 0.00

  NODE X Y Z THXY THYZ THZX
  21 16015. 12152. 112.19 0.00 0.00 0.00
  22 16015. 12152. 224.38 0.00 0.00 0.00
  23 16015. 12152. 336.57 0.00 0.00 0.00
  24 16015. 12152. 448.77 0.00 0.00 0.00
  25 16015. 12152. 560.96 0.00 0.00 0.00
  26 16015. 12152. 673.15 0.00 0.00 0.00
  27 16015. 12152. 785.34 0.00 0.00 0.00
  28 16015. 12152. 897.53 0.00 0.00 0.00
  29 16015. 12152. 1009.7 0.00 0.00 0.00
  30 16015. 12152. 1121.9 0.00 0.00 0.00
  31 16015. 12152. 1234.1 0.00 0.00 0.00
  32 16015. 12152. 1346.3 0.00 0.00 0.00
  33 16015. 12152. 1458.5 0.00 0.00 0.00
  34 16015. 12152. 1570.7 0.00 0.00 0.00
  35 15956. 12152. 1687.3 0.00 0.00 0.00
  36 15996. 12152. 1684.3 0.00 0.00 0.00
  37 15976. 12152. 1685.8 0.00 0.00 0.00
  38 15956. 12152. 1574.8 0.00 0.00 0.00
  39 15956. 12152. 1462.3 0.00 0.00 0.00
  40 15956. 12152. 1349.8 0.00 0.00 0.00


想用vb.net读取以上txt的数据,使得只读前四列,带字母的不要,如:
  1 15956. 12152. 0.000
  2 16015. 12152. 0.000
  3 15976. 12152. 0.000
  4 15996. 12152. 0.000
  5 16015. 12212. 0.000
  6 16015. 12172. 0.000
  7 16015. 12192. 0.000
  8 15956. 12212. 0.000
  9 15996. 12212. 0.000
  10 15976. 12212. 0.000
  11 15956. 12192. 0.000


  12 15956. 12172. 0.000
  13 15977. 12186. 0.000
  14 15983. 12202. 0.000
  15 15997. 12179. 0.000
  16 15980. 12165. 0.000
  17 15970. 12201. 0.000
  18 15997. 12197. 0.000
  19 16001. 12164. 0.000
  20 16015. 12152. 1682.9  
最主要的也就是把每行带字母的去掉,希望朋友们指点下,不胜感激!

[解决办法]
使用ReadLine方法,代码
http://msdn.microsoft.com/zh-cn/library/db5x7c0d.aspx
[解决办法]
s = sr.ReadLine()赋值给s然后SPlit,取前4个
[解决办法]
例子

VB.NET code
Dim sr As System.IO.StreamReader = New System.IO.StreamReader("d:\xx.txt", Encoding.GetEncoding("GB2312"))Dim str As StringDim arr As String()Dim xx As Int32Do  str = sr.ReadLine()  If Not (str Is Nothing) Then    arr = System.Text.RegularExpressions.Regex.Split(str, " +")    If Int32.TryParse(arr(0), xx) Then      Console.WriteLine( arr(0) + " " + arr(1) + " " + arr(2) + " " + arr(3))    End If  End IfLoop Until str Is Nothingsr.Close()
[解决办法]
If Int32.TryParse(a(0), xx) Then
再执行
c(m, n) = b(j, i)

[解决办法]
VB.NET code
Dim target As StringDim pattern As StringDim result As String'读取TXT数据target = System.IO.File.ReadAllText("d:\xx.txt", System.Text.Encoding.Default)'定义正则表达式pattern = "^\d{1,}( \d{1,}[.]\d{0,}){3}"'在读取到的数据中查找前四列数据result = ""For Each m As System.Text.RegularExpressions.Match In _    System.Text.RegularExpressions.Regex.Matches(target, pattern, RegexOptions.Multiline)    result &= m.Value & vbCrLfNext mMessageBox.Show(result)
[解决办法]
Dim fileContents As String
If OpenFileDialog1.ShowDialog = Windows.Forms.DialogResult.OK Then
Try
strFileName = OpenFileDialog1.FileName
fileContents = My.Computer.FileSystem.ReadAllText(strFileName, System.Text.Encoding.GetEncoding("GB2312"))
Catch ex As Exception
MessageBox.Show(ex.Message, My.Application.Info.Title, MessageBoxButtons.OK, MessageBoxIcon.Error)
End Try
End If

Dim strReader As New StringReader(fileContents)
Dim arrText As New ArrayList() '数组
Dim sLine As String = ""

Do
sLine = strReader.ReadLine()
If Not sLine Is Nothing Then '删除空行
arrText.Add(sLine) '逐行存入数据
End If
Loop Until sLine Is Nothing
strReader.Close()

热点排行