0

我正在尝试删除文本文件末尾的空行。该程序获取一个文件,对其进行操作并生成另一个文件。但是,我需要删除文件末尾的空白行...

   Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click
    ' Save to desktop if nothing is selected
    If txtDestLoc.Text = "" Then
        txtDestLoc.Text = "C:\Documents and Settings\" & LCase(Environment.UserName) & "\desktop"
    End If

    If txtFileLoc.Text <> "" Then

        Dim fsr As New FileStream(txtFileLoc.Text, FileMode.Open)
        Dim sr As New StreamReader(fsr)
        Dim sb As New System.Text.StringBuilder
        'Dim strHeader As String


        ' Get just file name
        Dim strFileName = Me.OpenFileDialog1.FileName()
        Dim fnPeices() As String = strFileName.Split("\")
        Dim fileName As String = ""

        fileName = "CCCPositivePay.txt"



        Dim strOutFile As String = txtDestLoc.Text & "\" & fileName
        Dim fsw As New FileStream(strOutFile, FileMode.Create, FileAccess.Write)
        Dim w As New StreamWriter(fsw)
        Dim i As Double

        Dim srRow As String
        Dim strW As String
        Dim strDate As String
        Dim strAmt As String
        Dim strChNo As String
        Dim strName As String
        Dim strAddInfo As String
        Dim strCustAcct As String
        Dim totamt As Double = 0
        Dim strAcct As String = "2000002297330"



        strLoc = txtDestLoc.Text()
        srRow = ""
        Do While sr.Peek() <> -1
            srRow = srRow.ToString & sr.ReadLine()
            If srRow.Length = 133 Then
                If srRow.Substring(131, 2) = "CR" Then


                    strCustAcct = srRow.Substring(2, 18).Replace("-", "")
                    strName = srRow.Substring(23, 35)
                    strAddInfo = srRow.Substring(23, 30)
                    strDate = srRow.Substring(103, 4) + srRow.Substring(97, 2) + srRow.Substring(100, 2)
                    strChNo = srRow.Substring(110, 10)
                    strAmt = strip(srRow.Substring(121, 10))


                    strW = strAcct + strChNo.Trim.PadLeft(10, "0") + strAmt.Trim.PadLeft(10, "0") + strDate + " " + strAddInfo + Space(8) + strName + Space(20)
                    sb.AppendLine(strW)

                    totamt = totamt + CDbl(strAmt)


                    i = i + 1
                End If

            End If
            srRow = ("")
        Loop


        'w.WriteLine(strHeader)
        w.WriteLine(sb.ToString)

        Dim file As String = txtFileLoc.Text
        Dim path As String = txtFileLoc.Text.Substring(0, File.lastindexof("\"))
        Dim strFileProcessed As String

        strFileProcessed = fnPeices(fnPeices.Length - 1)


        Label1.Text = "Refund File Processed:  " & strFileProcessed
        Label2.Text = "File saved to:  " & strOutFile


        ' Close everything
        w.Close()
        sr.Close()
        fsw.Close()
        fsr.Close()

        ' Move file after processing
        System.IO.File.Move(file, path + "\CB008_Processed\" + Now.ToString("MMddyyyyHHmm") + strFileProcessed)

        ' Put a copy of the results in "Processed" folder
        System.IO.File.Copy(strOutFile, path + "\CB008_Processed\" + Now.ToString("MMddyyyyHHmm") + fileName)


    Else
        MessageBox.Show("Please select a Refund file to process.", "CCC Refund File", MessageBoxButtons.OK)
    End If




End Sub
Public Function strip(ByVal des As String)
    Dim strorigFileName As String
    Dim intCounter As Integer
    Dim arrSpecialChar() As String = {".", ",", "<", ">", ":", "?", """", "/", "{", "[", "}", "]", "`", "~", "!", "@", "#", "$", "%", "^", "&", "*", "(", ")", "_", "-", "+", "=", "|", " ", "\"}
    strorigFileName = des
    intCounter = 0
    Dim i As Integer
    For i = 0 To arrSpecialChar.Length - 1

        Do Until intCounter = 29
            des = Replace(strorigFileName, arrSpecialChar(i), "")
            intCounter = intCounter + 1
            strorigFileName = des
        Loop
        intCounter = 0
    Next
    Return strorigFileName

End Function
4

1 回答 1

1

只做一个Writeline如果Not String.IsNullOrEmpty(sb)

于 2012-12-11T12:46:13.983 回答