如何将两个文本文件合并为一个文本文件?

时间:2017-02-14 01:05:42

标签: c# .net

第一个文本文件如下所示:

eu
alps
nl
de
sp
fr
gr
it
pl
scan

第二个文本文件如下所示:

Europe
Alps
Benelux
Germany
Spain & Portugal
France
Greece
Italy
Poland
Scandinavia

我想要读取这两个文本文件并以这种格式创建一个新的文本文件:

Code = eu
Country = Europe
Code = alps
Country = Alps
Code = nl
Country = Benelux

以这种格式继续其余部分。

到目前为止我所做的是从每个文件中读取行:但不知道如何继续。

IEnumerable f1 = File.ReadLines(@"c:\temp\txt1");
IEnumerable f2 = File.ReadLines(@"c:\temp\txt2");

5 个答案:

答案 0 :(得分:2)

压缩两个文件的相应行,选择代码和国家/地区字符串并在将其写入文件之前展平字符串数组:

File.WriteAllLines(@"c:\temp\txt3",
   f1.Zip(f2, (l1,l2) => new[] { $"Code = {l1}", $"Country = {l2}" }).SelectMany(a => a));

请勿忘记IEnumerable<string>f1使用f2类型。您还可以生成单个字符串:

File.WriteAllLines(@"c:\temp\txt3",
   f1.Zip(f2, (l1,l2) => $"Code = {l1}{Environment.NewLine}Country = {l2}" ));

输出:

Code = eu
Country = Europe
Code = alps
Country = Alps
Code = nl
Country = Benelux
Code = de
Country = Germany
Code = sp
Country = Spain & Portugal
Code = fr
Country = France
Code = gr
Country = Greece
Code = it
Country = Italy
Code = pl
Country = Poland
Code = scan
Country = Scandinavia

= Try Me =

答案 1 :(得分:1)

试试这个:

List<string> codeList = File.ReadLines(@"c:\temp\txt1").ToList();
List<string> nameList = File.ReadLines(@"c:\temp\txt2").ToList();

StringBuilder codeNameBuilder = new StringBuilder();

for (int i = 0; i < codeList.Count; i++)
{
    codeNameBuilder.AppendFormat("Code = {0} \n Country = {1}", codeList[i], nameList[i]);
}

System.IO.File.WriteAllText(@"c:\temp\txtOutput.txt", codeNameBuilder.ToString());

答案 2 :(得分:0)

我会这样做:

string[] f1 = File.ReadAllLines(@"c:\temp\txt1");
string[] f2 = File.ReadAllLines(@"c:\temp\txt2");
string[] f3 = new string[Math.Min(f1.Length, f2.Length)];

for (int i = 0; i < f3.Length; i++)
     f3[i] = "Code = " + f1[i] + "\nCountry = " + f2[i] +"\n";
File.WriteAllLines(@"c:\temp\txt3", f3);

答案 3 :(得分:0)

试试这个:

        string CodePath = Environment.CurrentDirectory + @"\Code.txt";
        List<string> Codes = File.ReadLines(CodePath).ToList();

        string CountryPath = Environment.CurrentDirectory + @"\Country.txt";
        List<string> Countries = File.ReadLines(CountryPath).ToList();

        string result = string.Empty;
        int length = Math.Min(Codes.Count, Countries.Count);
        for (int i = 0; i < length; i++)
        {
            result += "Code = " + Codes[i] + Environment.NewLine + "Country = " + Countries[i] + Environment.NewLine;
        }

        string OutPath = Environment.CurrentDirectory + @"\out.txt";
        File.WriteAllText(OutPath, result);

答案 4 :(得分:0)

方法1(3个阵列)

创建两个数组,然后创建第三个数组来存储连接的字符串:

var codes = File.ReadAllLines( "Codes.txt" );
var countries = File.ReadAllLines( "Countries.txt" );

var lines = new List<string>( codes.Length );
for( int i = 0; i < codes.Length; i++ ) {
   lines.Add( $"Code = {codes[ i ]}" + Environment.NewLine + $"Country = {countries[ i ]}" );
}

File.WriteAllLines( "Result.txt",  lines);

方法2(2阵列内存少)

如果由于内存消耗而不想要第三个数组,那么您可以使用已有的数组之一并将连接的字符串存储在其中:

var codes = File.ReadAllLines( "Codes.txt" );
var countries = File.ReadAllLines( "Countries.txt" );

for( int i = 0; i < codes.Length; i++ ) {
   codes[i] = $"Code = {codes[ i ]}" + Environment.NewLine + $"Country = {countries[ i ]}";
}

File.WriteAllLines( "Result.txt", codes );

这是最好的选择,因为它是两全其美的:更少的内存和只有3个IO操作(2次读取,1次写入)。

方法3(1阵列内存较小)

此技术将所有国家/地区读入内存,然后从代码文件中读取一行到内存中,将其写入目标文件,然后从代码文件中读取另一行。所以在任何给定的时间,内存中只有一个代码。这是使用File.ReadLines实现的。

var countries = File.ReadAllLines( "Countries.txt" );
int i = 0;
File.WriteAllLines( "Result2.txt", File.ReadLines( "Codes.txt" )
   .Select( x => $"Code = {x}" + Environment.NewLine + $"Country = {countries[ i++ ]}" ) );