I have managed to merge two text files together using this code
我已设法使用此代码将两个文本文件合并在一起
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
It merges them however I would like it to merge the two text files one line at a time. for example
它合并它们但是我希望它一次合并两个文本文件。例如
Textdoc1 contains
First Line
Third Line
Textdoc2 contains
Second Line
Fourth Line
I would like the output file to contain:
我想输出文件包含:
First line
Second Line
Third Line
Fourth Line
any help is very appreciated, thanks!
非常感谢任何帮助,谢谢!
1 个解决方案
#1
0
You'll have to use the ReadAllLines instead of ReadAllText. Here's a quick example to show you how it could work (I haven't tested this code, it's just for reference)
您必须使用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())
If you have very large file, then I suggest you look into using StreadReader instead. This way you can read a line without loading everything at once.
如果你有非常大的文件,那么我建议你考虑使用StreadReader。这样您就可以在不加载所有内容的情况下读取一行。
#1
0
You'll have to use the ReadAllLines instead of ReadAllText. Here's a quick example to show you how it could work (I haven't tested this code, it's just for reference)
您必须使用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())
If you have very large file, then I suggest you look into using StreadReader instead. This way you can read a line without loading everything at once.
如果你有非常大的文件,那么我建议你考虑使用StreadReader。这样您就可以在不加载所有内容的情况下读取一行。