有谁知道如何阅读文件中的文本块?这就是我要找的......
该文件类似于以下内容:
...
STANDARD 1.abc def 1.000 \
STAMP="0A7B 1258 AC82 470F 682B D06A 4FD5 \
A108 5A1B 474B A5D5 F241 B9C8 04A9 6455 0B0C E3B4 AFF6 39E5 \
A5AE F951 FF30 0870 288C F900 6C53 12B6 EBB3 3A3D 57A2" \
ADVANCED 1.abc ghi 1.000 \
STAMP="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
DE9A 6BFF 0E5E 06BE 477D EECF 83D6 069E ADAA 5BDC 8947 9A9B \
30F7 4042 C4EA BFC6 4ECF 993A EEE4 9180 13FE C8C7"
...
此模式在整个文件中重复,该文件在“STAMP”部分后面也有“STAMP2 =”部分。此外,ADVANCED依赖于STANDARD,这意味着两者都必须存在。我需要找到标准部分(这是一个简单的过程),然后找到指定的ADVANCED部分并将STAMP值(引号之间的所有内容)分配给字符串变量。
是否有简单的方法来完成此任务,如果是,请分享。
谢谢!
答案 0 :(得分:0)
这样的事情会对你有所帮助。您需要嵌套检查以找出现在正在读取的部分。
注意:这不是一个完整的代码,它只是一个让你有所了解的启动器。
Const STANDARD As String = "STANDARD"
Const ADVANCE As String = "ADVANCE"
Dim sectionIdentifier As String
Dim standardSection As New StringBuilder
Dim advanceSection As New StringBuilder
Private Sub Form1_Load(sender As System.Object, e As System.EventArgs) Handles MyBase.Load
ReadFile("YourFilePathHere")
End Sub
Sub ReadFile(ByVal path As String)
Dim reader As New StreamReader(path)
Dim line As String = String.Empty
While Not reader.EndOfStream
line = reader.ReadLine()
If (line.StartsWith(STANDARD)) Then
sectionIdentifier = line.Replace(STANDARD, "").Trim
ReadStandardSection(reader)
End If
End While
reader.Close()
End Sub
Sub ReadStandardSection(ByRef reader As StreamReader)
Dim line As String = String.Empty
While Not reader.EndOfStream
line = reader.ReadLine()
If (line.TrimStart().StartsWith("ADVANCE")) Then
Dim sectionidentiferAdv As String = line.Replace(ADVANCE, "").Trim
If (sectionIdentifier <> sectionidentiferAdv) Then
' display alert advance section for identifier not found
' possibly return from here, since file structure is not expected.
End If
Debug.Print(standardSection.ToString())
Debug.Print("standard end")
standardSection.Clear()
'' standard data can be use and clear
ReadAdvanceSection(reader)
Else
standardSection.AppendLine(line)
End If
End While
End Sub
Sub ReadAdvanceSection(ByRef reader As StreamReader)
Dim line As String = String.Empty
While Not reader.EndOfStream
line = reader.ReadLine()
If (line.StartsWith(STANDARD)) Then
Debug.Print(advanceSection.ToString())
Debug.Print("advance end")
advanceSection.Clear()
'' advance section data can be use and clear
Return
End If
advanceSection.AppendLine(line)
End While
If (reader.EndOfStream) Then
Debug.Print(advanceSection.ToString())
Debug.Print("advance end")
End If
End Sub
控制台日志:
STAMP="0A7B 1258 AC82 470F 682B D06A 4FD5 \
A108 5A1B 474B A5D5 F241 B9C8 04A9 6455 0B0C E3B4 AFF6 39E5 \
A5AE F951 FF30 0870 288C F900 6C53 12B6 EBB3 3A3D 57A2" \
STAMP2="0A7B 1258 AC82 470F 682B D06A 4FD5 \
A108 5A1B 474B A5D5 F241 B9C8 04A9 6455 0B0C E3B4 AFF6 39E5 \
A5AE F951 FF30 0870 288C F900 6C53 12B6 EBB3 3A3D 57A2" \
standard end
STAMP="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
STAMP="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
DE9A 6BFF 0E5E 06BE 477D EECF 83D6 069E ADAA 5BDC 8947 9A9B \
30F7 4042 C4EA BFC6 4ECF 993A EEE4 9180 13FE C8C7"
STAMP2="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
STAMP2="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
DE9A 6BFF 0E5E 06BE 477D EECF 83D6 069E ADAA 5BDC 8947 9A9B \
30F7 4042 C4EA BFC6 4ECF 993A EEE4 9180 13FE C8C7"
advance end
STAMP="0B7B 1258 AC82 470F 682B D06A 4FD5 \
A108 5A1B 474B A5D5 F241 B9C8 04A9 6455 0B0C E3B4 AFF6 39E5 \
A5AE F951 FF30 0870 288C F900 6C53 12B6 EBB3 3A3D 57A2" \
STAMP2="0B7B 1258 AC82 470F 682B D06A 4FD5 \
A108 5A1B 474B A5D5 F241 B9C8 04A9 6455 0B0C E3B4 AFF6 39E5 \
A5AE F951 FF30 0870 288C F900 6C53 12B6 EBB3 3A3D 57A2" \
standard end
STAMP="A76F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
STAMP="A76F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
DE9A 6BFF 0E5E 06BE 477D EECF 83D6 069E ADAA 5BDC 8947 9A9B \
30F7 4042 C4EA BFC6 4ECF 993A EEE4 9180 13FE C8C7"
STAMP2="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
STAMP2="076F 1C48 BBC2 0779 EB13 2063 E61D 1DB4 \
DE9A 6BFF 0E5E 06BE 477D EECF 83D6 069E ADAA 5BDC 8947 9A9B \
30F7 4042 C4EA BFC6 4ECF 993A EEE4 9180 13FE C8C7"
advance end