文本文檔使用如何到新的生產線在使用VB.Net
File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", "This is the second line")
如何使文本的第二行出現在第一位的,因爲如果我打回車鍵?這樣做只是簡單地把第二行放在第一行的旁邊。
文本文檔使用如何到新的生產線在使用VB.Net
File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", "This is the second line")
如何使文本的第二行出現在第一位的,因爲如果我打回車鍵?這樣做只是簡單地把第二行放在第一行的旁邊。
使用Environment.NewLine
File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", Environment.NewLine + "This is the second line")
或者你可以使用StreamWriter
Using writer As new StreamWriter("mytextfile.text", true)
writer.WriteLine("This is the first line")
writer.WriteLine("This is the second line")
End Using
可能:
File.AppendAllText("c:\mytextfile.text", "This is the first line")
File.AppendAllText("c:\mytextfile.text", vbCrLf & "This is the second line")
vbCrLf
是一個換行符常數。
如果你有很多的這個呼籲它的方式更好地使用StringBuilder:
Dim sb as StringBuilder = New StringBuilder()
sb.AppendLine("This is the first line")
sb.AppendLine("This is the second line")
sb.AppendLine("This is the third line")
....
' Just one call to IO subsystem
File.AppendAllText("c:\mytextfile.text", sb.ToString())
如果您有很多很多的字符串可以編寫,然後你可以用方法來包裝所有的東西。
Private Sub AddTextLine(ByVal sb As StringBuilder, ByVal line as String)
sb.AppendLine(line)
If sb.Length > 100000 then
File.AppendAllText("c:\mytextfile.text", sb.ToString())
sb.Length = 0
End If
End Sub
這意味着使用內存中的完整字符串,這取決於大小,這可能是一個問題。 – Magnus 2012-04-10 19:44:23
@Magnus是的,但可以很容易地處理一些中間調用AppendAllText。看到我更新的答案 – Steve 2012-04-10 19:50:37