VB.NET读取PM文件,判断格式并提取数据
{/'title/':/'VB.NET读取PM文件,判断格式并提取数据/',/'description/':/'使用VB.NET读取PM文件,判断第一行是否为“HINTCAD5.83_PM_SHUJU_PM”,如果不是则提示文件格式不对。从第4行开始,每隔3行读取7个数据(空格不算)存入数组,如果一组数据都为0则停止读取,并将读取的数据写入新的文件。/',/'keywords/':/'VB.NET, PM文件, 文件读取, 数据提取, 格式校验/',/'content/':/'Imports System.IO//n//nModule Module1//n//n Sub Main()//n Dim filePath As String = ///'文件路径///' ' 替换为你的PM文件路径//n Dim outputFilePath As String = ///'输出文件路径///' ' 替换为你想要输出的文件路径//n//n ' 读取PM文件内容//n Dim lines As String() = File.ReadAllLines(filePath)//n//n ' 判断第一行是否为指定字符串//n If lines.Length > 0 AndAlso lines(0) = ///'HINTCAD5.83_PM_SHUJU_PM///' Then//n ' 存储读取的数据//n Dim data As New List(Of Integer)//n//n ' 从第4行开始读取数据//n Dim startIndex As Integer = 3//n//n ' 每间隔3行读取一组数据//n While startIndex + 6 < lines.Length ' 至少还有7行数据//n Dim zeroCount As Integer = 0 ' 用于计算每组数据中0的个数//n//n ' 读取每一组数据//n For i As Integer = startIndex To startIndex + 6//n Dim line As String = lines(i)//n Dim value As Integer//n//n ' 尝试将每行数据转换为整数//n If Integer.TryParse(line, value) Then//n data.Add(value)//n//n ' 统计0的个数//n If value = 0 Then//n zeroCount += 1//n End If//n End If//n Next//n//n ' 如果一组数据中所有值都为0,则停止读取//n If zeroCount = 7 Then//n Exit While//n End If//n//n startIndex += 9 ' 加9,跳过未读取的3行//n End While//n//n ' 将读取的数据写入文件//n File.WriteAllLines(outputFilePath, data.Select(Function(d) d.ToString()))//n Else//n Console.WriteLine(///'文件格式不正确///')//n End If//n End Sub//n//nEnd Module/
原文地址: https://www.cveoy.top/t/topic/pMIv 著作权归作者所有。请勿转载和采集!