如何删除vb.net中文本文件末尾的行?
How to delete the line at the end of the text file in vb.net?
如何确保文件末尾没有多余的一行?有点特殊,我把一行分成了三个地方,分别是17、90、120字节(共227字节)。我尝试用 Write 替换 WriteLine,没问题,但之后我不能写新行了!
我当前的代码:
Dim bibliotheque As New article
With bibliotheque
.Title = TextBox1.Text
.Name = TextBox2.Text
.Charge = TextBox3.Text
End With
Dim fileName As String = "c:\essai.librairie"
Dim fs As FileStream = Nothing
Try
fs = New FileStream(fileName, FileMode.Append)
Using writer As StreamWriter = New StreamWriter(fs)
writer.WriteLine(bibliotheque.Title.PadRight(17, " "c).ToString & bibliotheque.Name.PadRight(90, " "c).ToString & bibliotheque.Charge.PadRight(120, " "c).ToString)
ListBox1.Items.Add(bibliotheque.Name)
End Using
Finally
If fs IsNot Nothing Then
fs.Dispose()
End If
End Try
您可以将 WriteLine
替换为 Write
,使用布尔变量来了解您是否已经添加了第一行,如果已经添加了第一行,则只添加回车符 return/line 在添加内容之前使用带有空字符串的 WriteLine
进行提要,例如:
Dim linesExist As Boolean = IO.File.ReadAllLines(fileName).Count > 0
Try
fs = New FileStream(fileName, FileMode.Append)
Using writer As StreamWriter = New StreamWriter(fs)
If linesExist Then writer.WriteLine()
writer.Write(bibliotheque.Title.PadRight(17, " "c).ToString & bibliotheque.Name.PadRight(90, " "c).ToString & bibliotheque.Charge.PadRight(120, " "c).ToString)
ListBox1.Items.Add(bibliotheque.Name)
End Using
Finally
If fs IsNot Nothing Then
fs.Dispose()
End If
End Try
显然,File.ReadAllLines
可能会很慢,您可以使用更优化的方法来检测文件是否存在以及是否有内容,但它确实解决了手头的问题。
Dim fileName As String = "c:\essai.librairie"
Dim bt As New article With {
.Title = TextBox1.Text,
.Name = TextBox2.Text,
.Charge = TextBox3.Text
}
Using fs As New FileStream(fileName, FileMode.Append)
Using writer As New StreamWriter(fs)
If fs.Length > 0 Then writer.WriteLine()
writer.Write($"{bt.Title,-17}{bt.Name,-90}{bt.Charge,-120}")
End Using
End Using
ListBox1.Items.Add(bt.Name)
如何确保文件末尾没有多余的一行?有点特殊,我把一行分成了三个地方,分别是17、90、120字节(共227字节)。我尝试用 Write 替换 WriteLine,没问题,但之后我不能写新行了! 我当前的代码:
Dim bibliotheque As New article
With bibliotheque
.Title = TextBox1.Text
.Name = TextBox2.Text
.Charge = TextBox3.Text
End With
Dim fileName As String = "c:\essai.librairie"
Dim fs As FileStream = Nothing
Try
fs = New FileStream(fileName, FileMode.Append)
Using writer As StreamWriter = New StreamWriter(fs)
writer.WriteLine(bibliotheque.Title.PadRight(17, " "c).ToString & bibliotheque.Name.PadRight(90, " "c).ToString & bibliotheque.Charge.PadRight(120, " "c).ToString)
ListBox1.Items.Add(bibliotheque.Name)
End Using
Finally
If fs IsNot Nothing Then
fs.Dispose()
End If
End Try
您可以将 WriteLine
替换为 Write
,使用布尔变量来了解您是否已经添加了第一行,如果已经添加了第一行,则只添加回车符 return/line 在添加内容之前使用带有空字符串的 WriteLine
进行提要,例如:
Dim linesExist As Boolean = IO.File.ReadAllLines(fileName).Count > 0
Try
fs = New FileStream(fileName, FileMode.Append)
Using writer As StreamWriter = New StreamWriter(fs)
If linesExist Then writer.WriteLine()
writer.Write(bibliotheque.Title.PadRight(17, " "c).ToString & bibliotheque.Name.PadRight(90, " "c).ToString & bibliotheque.Charge.PadRight(120, " "c).ToString)
ListBox1.Items.Add(bibliotheque.Name)
End Using
Finally
If fs IsNot Nothing Then
fs.Dispose()
End If
End Try
显然,File.ReadAllLines
可能会很慢,您可以使用更优化的方法来检测文件是否存在以及是否有内容,但它确实解决了手头的问题。
Dim fileName As String = "c:\essai.librairie"
Dim bt As New article With {
.Title = TextBox1.Text,
.Name = TextBox2.Text,
.Charge = TextBox3.Text
}
Using fs As New FileStream(fileName, FileMode.Append)
Using writer As New StreamWriter(fs)
If fs.Length > 0 Then writer.WriteLine()
writer.Write($"{bt.Title,-17}{bt.Name,-90}{bt.Charge,-120}")
End Using
End Using
ListBox1.Items.Add(bt.Name)