嘿,所以我试图做一个简单的StreamWriter来向文本文件添加工作人员信息(我已经有一个有效的StreamReader登录页面),该文本文件具有一些示例数据,但还必须能够具有新数据写给它。
我的问题是,仅在初次启动和最初写入数据时,信息才与文本文件中样本数据的最后一行写在同一行上,而不是在下一个空白行上写。 这是启动后第一次尝试写入数据后文本文件的外观。
“ Me,Too”是我的示例数据的最后一行,“ Grey,Hardy”是我的第一个通过StreamWriter编写的数据
我在这里进行了一些研究,找到了如何编写空白行的示例,可以使StreamWriter每次在空白行之间放置空格,但这在这里不起作用,因为此问题仅在第一次在任何给定的启动程序中运行,如果每次都留空行,那么如果我两次尝试使用StreamWriter,它将看起来像
然后将启用空白登录。
这是我的StreamWriter代码
'StreamReader to check if a line is empty or not
Dim StaffReader As StreamReader = File.OpenText("StaffInfo.txt")
strLine = StaffReader.ReadLine()
StaffReader.Close()
'Streamwriter for adding staff log in to textfile
Dim FileCheck As StreamWriter = File.AppendText("StaffInfo.txt")
FileCheck.WriteLine(strStaffUsername2 & "," & strStaffPass2)
FileCheck.Close(``)
MessageBox.Show(strStaffUsername2 & " added to the file. ", "Data Added")
txtUser2.Clear()
txtUser2.Focus()
txtPass2.Clear()
如果要写的行上有任何字符,我只是希望它跳到下一个空白行。
答案 0 :(得分:1)
如果文件中的最后一行是否包含回车符,便是要查找的要点。如果它确实包含回车符,则可以在文本后面附加书面代码。但是,如果其中不包含回车符,则必须在第一行前面加上回车符。
因此,首先,您必须检查最后一行是否有回车符。您可以通过以下函数来做到这一点:
Private Function DoesLastLineHasCarriagReturn(Filename As String) As Boolean
Dim s as String = ""
using stream = New StreamReader(Filename)
' we have to read the last two characters
stream.BaseStream.Seek(-2,SeekOrigin.End)
dim c_beforeLast as Integer = stream.Read()
Dim c_Last As Integer = stream.Read()
' if the last character is 10 -> we have a carriage return. Windows and Linux just use different encodings
if (c_Last =10)
If(c_beforeLast = 13)
Console.WriteLine("Ends with carriage return CRLF (Windows)")
Else
Console.WriteLine("Ends with carriage return LF (UNIX, OSX )")
End If
return true
Else
Console.WriteLine("Does not end with Carriage return")
End If
End Using
return false
End Function
然后,当开始写入文件时,首先必须调用该函数。代码如下所示:
If DoesLastLineHasCarriagReturn("StaffInfo.txt")
Dim FileCheck As StreamWriter = File.AppendText("StaffInfo.txt")
FileCheck.WriteLine("Greg" & "," & "Hardy")
FileCheck.Close()
Else
Dim FileCheck As StreamWriter = File.AppendText("StaffInfo.txt")
FileCheck.WriteLine(vbCrLf + "Greg" & "," & "Hardy")
FileCheck.Close()
End If
顺便说一句,我建议您使用using
语句自动关闭流:
If DoesLastLineHasCarriagReturn("StaffInfo.txt")
Using writer As New StreamWriter("StaffInfo.txt", True)
writer.WriteLine("Greg" & "," & "Hardy")
End Using
Else
Using writer As New StreamWriter("StaffInfo.txt", True)
writer.WriteLine(vbCrLf +"Greg" & "," & "Hardy")
End Using
End If
答案 1 :(得分:0)
由于我们正在处理文本文件并且没有做任何花哨的操作,因此我们只能在System.IO中使用File类。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim FilePath = "TestFiles/File1.txt"
'Dump contents of file
Dim fileContents As String = File.ReadAllText(FilePath)
'Extract the last 2 characters in the contents
Dim NextToLast As String = fileContents(fileContents.Length - 2)
Dim LastCharacter As String = fileContents(fileContents.Length - 1)
'Check if we have a CrLf an if not add a new line
If Not NextToLast = Chr(13) AndAlso Not LastCharacter = Chr(10) Then
File.WriteAllText(FilePath, Environment.NewLine)
End If
'Write to the file
File.AppendAllLines(FilePath, {TextBox1.Text})
End Sub
要显示文本文件,请在列表中添加一个列表框。
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
'Put the contents of the file into a string array of lines
Dim staff() As String = File.ReadAllLines("TestFiles/File1.txt")
ListBox1.DataSource = staff
End Sub