My.Computer.FileSystem对象出现问题

时间:2019-06-06 15:24:59

标签: string vb.net file

在任务中,我从一个文件读取数据,对其进行处理,然后写入另一个文件。 处理过程是搜索分数为零的实数记录,这些实数必须写入另一个文件。例如123.000-写,12b.0-不写

而且,此程序可与OpenFileDialog一起使用,但不适用于My.Computer.FileSystem

我尝试使用vbnewline-separator进行分割数组,但这没有帮助。 当我逐行进行调试并且程序达到所需的数字(输入为123.0)时,不考虑标志1,这就是为什么数字未传递到结果中的原因

        Try
            content = My.Computer.FileSystem.ReadAllText(fileName)
        Catch ex As Exception
            MsgBox("Error:" & vbCrLf & vbCrLf & ex.Message)
        End Try
    End Sub
    Sub writeFile(ByVal fileName As String, ByRef content As String)
        Try
            My.Computer.FileSystem.WriteAllText(fileName, content, False)
        Catch ex As Exception
            MsgBox("Error")
        End Try
    End Sub
    Sub ch(ByVal Str As String, ByRef Result As String)
        Dim k, i, m, flag1, flag2 As Integer
        Result = ""
        k = Str.IndexOf(".", 1)
        m = Str.Length - 1
        If k <> -1 Then
            flag1 = 0
            flag2 = 0
            For i = 0 To k - 1
                If Str.Substring(i, 1) >= "0" And Str.Substring(i, 1) <= "9" Then
                    flag1 = flag1 + 1
                Else
                    Exit For
                End If
            Next
            If flag1 = k Then
                For i = k + 1 To m
                    If Str.Substring(i, 1) = "0" Then
                        flag2 = flag2 + 1
                    Else
                        Exit For
                    End If
                Next
            Else
            End If
            If flag2 = m - k And flag2 <> 0 Then
                Result = Str
            Else
            End If
        Else
        End If
    End Sub
    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        Dim line, line2, path, path2, res, temp As String
        temp = ""
        line = ""
        line2 = ""
        path = ""
        path2 = ""
        res = ""
        path = TextBox1.Text
        path2 = TextBox2.Text
        readFile(path, line)
        TextBox3.Text = CStr(line)
        Dim mass = line.Split(CChar(vbNewLine))
        For i As Integer = 0 To UBound(mass)
            ch(mass(i), temp)
            line2 = line2 + temp
        Next
        TextBox4.Text = line2
        writeFile(path2, line2)
    End Sub

输入:123

123。

123.0

123.0a0

123.000

12g.00

(6个字符串,结尾没有空格) 输出:空文件

2 个答案:

答案 0 :(得分:0)

这是另一种方法:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim InputFileName As String = TextBox1.Text
    Try
        Dim lines() As String = System.IO.File.ReadAllLines(InputFileName)
        Dim RealNumbers As List(Of String) = FindRealNumbersEndingInZeroOnly(lines)
        Dim OutputFileName As String = TextBox2.Text
        Try
            System.IO.File.WriteAllLines(OutputFileName, RealNumbers)
            MessageBox.Show("Done!")
        Catch ex As Exception
            MessageBox.Show("FileName: " & OutputFileName & vbCrLf & vbCrLf & "Error: " & ex.Message, "Error Writing File")
        End Try
    Catch ex As Exception
        MessageBox.Show("FileName: " & InputFileName & vbCrLf & vbCrLf & "Error: " & ex.Message, "Error Reading File")
    End Try
End Sub

Public Function FindRealNumbersEndingInZeroOnly(ByVal lines() As String) As List(Of String)
    Dim matches As New List(Of String)
    For Each line As String In lines ' check each line from the file
        Dim startAt As Integer = 0
        Dim number As String = ".0" ' bare minimum starter for a real number
        Dim periodFollowedByZero As Integer = line.IndexOf(number, startAt)
        While periodFollowedByZero <> -1 ' there might be more than one real number per line
            ' seach backwards from the period, pre-pending any digits found
            For before As Integer = (periodFollowedByZero - 1) To 0 Step -1
                Dim ch As String = line.Substring(before, 1)
                If Char.IsDigit(ch) Then
                    number = ch & number
                Else
                    Exit For
                End If
            Next

            If Not number.StartsWith(".") Then ' if we don't have a period at the beginning we have a number!
                ' search forwards from the ".0", appending any digits found; stop if non-zero digit is found
                Dim zeroesOnlyAfterwards As Boolean = True ' assume true until proven otherwise
                For after As Integer = (periodFollowedByZero + 2) To (line.Length - 1)
                    Dim ch As String = line.Substring(after, 1)
                    If Char.IsDigit(ch) Then
                        If ch = "0" Then
                            number = number & ch
                        Else
                            zeroesOnlyAfterwards = False
                            Exit For
                        End If
                    Else
                        Exit For
                    End If
                Next

                If zeroesOnlyAfterwards Then
                    ' add the found number to our results
                    matches.Add(number)
                End If
            End If

            ' see if there are any more candidates on this line
            startAt = periodFollowedByZero + 2
            periodFollowedByZero = line.IndexOf(".", startAt)
        End While
    Next
    Return matches
End Function

答案 1 :(得分:0)

您的代码对我来说很好。

在这里,使用ReadAllText()进行了一些重做,而Split()进行了一些改动:

sap.m.Switch

我的输入文件:

Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
    Dim InputFileName As String = TextBox1.Text
    Dim lines() As String = My.Computer.FileSystem.ReadAllText(InputFileName).Split(vbCrLf.ToCharArray, StringSplitOptions.RemoveEmptyEntries)

    Dim line2 As String = ""
    For Each line As String In lines
        Dim result As String = ""
        ch(line, result)
        If result <> "" Then
            line2 = line2 + result + vbCrLf
        End If
    Next
    Debug.Print(line2)
End Sub

Sub ch(ByVal Str As String, ByRef Result As String)
    Dim k, i, m, flag1, flag2 As Integer
    Result = ""
    k = Str.IndexOf(".", 1)
    m = Str.Length - 1
    If k <> -1 Then
        flag1 = 0
        flag2 = 0
        For i = 0 To k - 1
            If Str.Substring(i, 1) >= "0" And Str.Substring(i, 1) <= "9" Then
                flag1 = flag1 + 1
            Else
                Exit For
            End If
        Next
        If flag1 = k Then
            For i = k + 1 To m
                If Str.Substring(i, 1) = "0" Then
                    flag2 = flag2 + 1
                Else
                    Exit For
                End If
            Next
        End If
        If flag2 = m - k And flag2 <> 0 Then
            Result = Str
        End If
    End If
End Sub

立即窗口中的输出:

123
123.
123.0
123.0a0
123.000
12g.00