Vb.net 删除文件末尾的空行 我试图删除文本文件末尾的空行。程序获取一个文件,对其进行操作并生成另一个文件。然而,在文件的末尾有一些空白行,我需要去掉它们 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 Private子按钮3\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮3。单击 '如果未选择任何内容,则保存到桌面 如果txtDestLoc.Text=”“,则 txtDestLoc.Text=“C:\Documents and Settings\”&LCase(Environment.UserName)和“\desktop” 如果结束 如果txtFileLoc.Text为“”,则 将fsr设置为新文件流(txtFileLoc.Text,FileMode.Open) Dim sr作为新的StreamReader(fsr) 将sb设置为新System.Text.StringBuilder '作为字符串的Dim strHeader '仅获取文件名 Dim strFileName=Me.OpenFileDialog1.FileName() Dim FNPECES()作为字符串=strFileName.Split(“\”) Dim fileName As String=“” fileName=“CCCPositivePay.txt” Dim strOutFile As String=txtDestLoc.Text&“\”文件名 将fsw设置为新文件流(strOutFile、FileMode.Create、FileAccess.Write) Dim w作为新的StreamWriter(fsw) 我是双人的 将行设置为字符串 作为字符串的Dim strW 作为字符串的Dim strDate 作为字符串的暗带 Dim strChNo As字符串 将strName设置为字符串 作为一根弦 作为字符串的Dim strucstact 双精度总金额=0 Dim strAcct As String=“2000002297330” strLoc=txtDestLoc.Text() srRow=“” 执行以下操作:sr.Peek()-1 srRow=srRow.ToString和sr.ReadLine() 如果srRow.Length=133,则 如果srRow.Substring(131,2)=“CR”,则 strustact=srRow.Substring(2,18)。替换(“-”,”) strName=srRow.Substring(23,35) STRADINFO=srRow.Substring(23,30) strDate=srRow.Substring(103,4)+srRow.Substring(97,2)+srRow.Substring(100,2) strChNo=srRow.Substring(110,10) strAmt=条带(srRow.子串(121,10)) strW=strAcct+strChNo.Trim.PadLeft(10,“0”)+strAmt.Trim.PadLeft(10,“0”)+strDate+stradinfo+Space(8)+strName+Space(20) sb.附录行(strW) 总金额=总金额+CDbl(strAmt) i=i+1 如果结束 如果结束 srRow=(“”) 环 “w.WriteLine(斯特哈德) w、 写线(某人写线) Dim文件格式为String=txtFileLoc.Text Dim路径为String=txtFileLoc.Text.Substring(0,File.lastindexof(“\”)) 作为字符串处理的Dim STRFILE strFileProcessed=fnpeces(fnpeces.Length-1) Label1.Text=“已处理退款文件:”&strFileProcessed Label2.Text=“文件保存到:”&strOutFile "关上一切, w、 关闭() 高级关闭() fsw.Close() fsr.Close() '处理后移动文件 System.IO.File.Move(文件,路径+“\CB008\u已处理\”+Now.ToString(“mmddyyyhhmm”)+strFileProcessed) '将结果的副本放在“已处理”文件夹中 System.IO.File.Copy(strOutFile,路径+“\CB008\u已处理\”+Now.ToString(“mmddyyyhhmm”)+文件名) 其他的 Show(“请选择要处理的退款文件。”,“CCC退款文件”,MessageBox按钮。确定) 如果结束 端接头 公共功能条(ByVal des作为字符串) Dim strorigFileName作为字符串 整数计数器 将arrSpecialChar()作为字符串={,“,”,“,”,“:”,“?”,“/”,“{”,“[”,“},”,“],“`,“~”,“!”,“@”,“#”,“$”,“%,“^”,“&”,“*”,“(“,”,“,”,“-”,“+”,“=”,“,“,”,“,“,”,“,”,“,”,“} strorigFileName=des intCounter=0 作为整数的Dim i 对于i=0到arrSpecialChar.Length-1 直到intCounter=29为止 des=Replace(strorigFileName,arrsecialchar(i),“”) intCounter=intCounter+1 strorigFileName=des 环 intCounter=0 下一个 返回strorigFileName 端函数

Vb.net 删除文件末尾的空行 我试图删除文本文件末尾的空行。程序获取一个文件,对其进行操作并生成另一个文件。然而,在文件的末尾有一些空白行,我需要去掉它们 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 Private子按钮3\u单击(ByVal sender作为System.Object,ByVal e作为System.EventArgs)处理按钮3。单击 '如果未选择任何内容,则保存到桌面 如果txtDestLoc.Text=”“,则 txtDestLoc.Text=“C:\Documents and Settings\”&LCase(Environment.UserName)和“\desktop” 如果结束 如果txtFileLoc.Text为“”,则 将fsr设置为新文件流(txtFileLoc.Text,FileMode.Open) Dim sr作为新的StreamReader(fsr) 将sb设置为新System.Text.StringBuilder '作为字符串的Dim strHeader '仅获取文件名 Dim strFileName=Me.OpenFileDialog1.FileName() Dim FNPECES()作为字符串=strFileName.Split(“\”) Dim fileName As String=“” fileName=“CCCPositivePay.txt” Dim strOutFile As String=txtDestLoc.Text&“\”文件名 将fsw设置为新文件流(strOutFile、FileMode.Create、FileAccess.Write) Dim w作为新的StreamWriter(fsw) 我是双人的 将行设置为字符串 作为字符串的Dim strW 作为字符串的Dim strDate 作为字符串的暗带 Dim strChNo As字符串 将strName设置为字符串 作为一根弦 作为字符串的Dim strucstact 双精度总金额=0 Dim strAcct As String=“2000002297330” strLoc=txtDestLoc.Text() srRow=“” 执行以下操作:sr.Peek()-1 srRow=srRow.ToString和sr.ReadLine() 如果srRow.Length=133,则 如果srRow.Substring(131,2)=“CR”,则 strustact=srRow.Substring(2,18)。替换(“-”,”) strName=srRow.Substring(23,35) STRADINFO=srRow.Substring(23,30) strDate=srRow.Substring(103,4)+srRow.Substring(97,2)+srRow.Substring(100,2) strChNo=srRow.Substring(110,10) strAmt=条带(srRow.子串(121,10)) strW=strAcct+strChNo.Trim.PadLeft(10,“0”)+strAmt.Trim.PadLeft(10,“0”)+strDate+stradinfo+Space(8)+strName+Space(20) sb.附录行(strW) 总金额=总金额+CDbl(strAmt) i=i+1 如果结束 如果结束 srRow=(“”) 环 “w.WriteLine(斯特哈德) w、 写线(某人写线) Dim文件格式为String=txtFileLoc.Text Dim路径为String=txtFileLoc.Text.Substring(0,File.lastindexof(“\”)) 作为字符串处理的Dim STRFILE strFileProcessed=fnpeces(fnpeces.Length-1) Label1.Text=“已处理退款文件:”&strFileProcessed Label2.Text=“文件保存到:”&strOutFile "关上一切, w、 关闭() 高级关闭() fsw.Close() fsr.Close() '处理后移动文件 System.IO.File.Move(文件,路径+“\CB008\u已处理\”+Now.ToString(“mmddyyyhhmm”)+strFileProcessed) '将结果的副本放在“已处理”文件夹中 System.IO.File.Copy(strOutFile,路径+“\CB008\u已处理\”+Now.ToString(“mmddyyyhhmm”)+文件名) 其他的 Show(“请选择要处理的退款文件。”,“CCC退款文件”,MessageBox按钮。确定) 如果结束 端接头 公共功能条(ByVal des作为字符串) Dim strorigFileName作为字符串 整数计数器 将arrSpecialChar()作为字符串={,“,”,“,”,“:”,“?”,“/”,“{”,“[”,“},”,“],“`,“~”,“!”,“@”,“#”,“$”,“%,“^”,“&”,“*”,“(“,”,“,”,“-”,“+”,“=”,“,“,”,“,“,”,“,”,“,”,“} strorigFileName=des intCounter=0 作为整数的Dim i 对于i=0到arrSpecialChar.Length-1 直到intCounter=29为止 des=Replace(strorigFileName,arrsecialchar(i),“”) intCounter=intCounter+1 strorigFileName=des 环 intCounter=0 下一个 返回strorigFileName 端函数,vb.net,Vb.net,如果不是字符串,则只执行写入行。IsNullOrEmpty(sb)srRow是字符串,为什么要调用它上的ToString?好的,我取出了srRow上的ToString。已处理文件的末尾仍有2个空行。根据代码,它不会写入空行。由于您使用w.WriteLine(),因此它将在文件末尾写入。请尝试w.Write(sb.toString())。您是说原始文件末尾有空行,还是说您的代码正在输出原始文件中没有的额外空行?原始文件中有一个空行。我输出的文件有两个空行。URLReader-我将此W.WriteL

如果
不是字符串,则只执行
写入行
。IsNullOrEmpty(sb)
srRow是字符串,为什么要调用它上的ToString?好的,我取出了srRow上的ToString。已处理文件的末尾仍有2个空行。根据代码,它不会写入空行。由于您使用w.WriteLine(),因此它将在文件末尾写入。请尝试w.Write(sb.toString())。您是说原始文件末尾有空行,还是说您的代码正在输出原始文件中没有的额外空行?原始文件中有一个空行。我输出的文件有两个空行。URLReader-我将此W.WriteLine切换为W.Write,从而去掉了代码生成的额外行,现在我只需删除剩余的额外行。此答案删除了上一个文件生成的行。谢谢很高兴我能帮忙。我已经多次点击这个,当我不确定String对象是否包含任何信息时,我会执行If Not String.IsNullOrEmpty()。