我已设法使用此代码将两个文本文件合并在一起
For Each foundFile As String In
My.Computer.FileSystem.ReadAllText("path")
foundFile = foundFile
My.Computer.FileSystem.WriteAllText("path", foundFile, True)
Next
extraline = vbCrLf
My.Computer.FileSystem.WriteAllText("path", extraline, True)
My.Computer.FileSystem.WriteAllText("path", extraline, True)
For Each foundFile2 As String In
My.Computer.FileSystem.ReadAllText("path")
foundFile2 = foundFile2
My.Computer.FileSystem.WriteAllText("path", foundFile2, True)
Next
它合并它们但是我希望它一次合并两个文本文件。例如
Textdoc1包含
First Line
Third Line
Textdoc2包含
Second Line
Fourth Line
我希望输出文件包含:
First line
Second Line
Third Line
Fourth Line
非常感谢任何帮助,谢谢!
答案 0 :(得分:0)
您必须使用ReadAllLines而不是ReadAllText。这是一个快速示例,向您展示它是如何工作的(我没有测试过这段代码,仅供参考)
Dim linesFromFile1() As String
Dim linesFromFile2() As String
Dim combinedLines As New List(Of String)
linesFromFile1 = System.IO.File.ReadAllLines("file1")
linesFromFile2 = System.IO.File.ReadAllLines("file2")
For linePos As Integer = 0 To System.Math.Max(linesFromFile1.Length, linesFromFile2.Length) - 1
If linePos < linesFromFile1.Length Then combinedLines.Add(linesFromFile1(linePos))
If linePos < linesFromFile2.Length Then combinedLines.Add(linesFromFile2(linePos))
Next
System.IO.File.WriteAllLines("file3", combinedLines.ToArray())
如果您的文件非常大,我建议您考虑使用StreadReader。这样您就可以read a line一次性加载所有内容。