2012-12-10 289 views
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 
+0

srRow是一個字符串,你爲什麼叫的ToString就可以了? –

+0

好吧,我拿出了srRow上的ToString。處理文件末尾仍有2個空行。代碼中的 –

+0

,它不會寫空行。它將在文件末尾寫入\ r \ n,因爲您使用w.WriteLine()。嘗試w.Write(sb.toString())。 – urlreader

回答

0

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

+0

該答案刪除了前一個文件生成的行。謝謝! –

+0

很高興我能幫到你。我已經多次打過這個,並且當我不確定字符串對象是否包含任何信息時,我會執行If Not String.IsNullOrEmpty()。 –