为什么这个文件上没有换行符?
本文关键字:换行符 文件 为什么 | 更新日期: 2023-09-27 18:13:49
我编写了一些代码来比较两个文件,并将它们的公共行写入第三个文件。由于某种原因,包含公共行的第三个文件在一行上写入了所有的公共行。我甚至尝试添加Console.WriteLine(''n');添加一条新线来分隔公共线,但这没有帮助。有什么问题吗?
//This program will read files and compares to see if they have a line in common
//if there is a line in common then it writes than common line to a new file
static void Main(string[] args)
{
int counter = 0;
string line;
string sline;
string[] words;
string[] samacc = new string[280];
//first file to compare
System.IO.StreamReader sfile =
new System.IO.StreamReader("C:''Desktop''autoit''New folder''userlist.txt");
while ((sline = sfile.ReadLine()) != null)
{
samacc[counter] = sline;
Console.WriteLine();
counter++;
}
sfile.Close();
//file to write common lines to.
System.IO.StreamWriter wfile = new System.IO.StreamWriter("C:''Desktop''autoit''New folder''KenUserList.txt");
counter = 0;
//second file to compare
System.IO.StreamReader file =
new System.IO.StreamReader("C:''Desktop''autoit''New folder''AllUserHomeDirectories.txt");
while ((line = file.ReadLine()) != null)
{
words = line.Split(''t');
foreach (string i in samacc)
{
if (words[0] == i)
{
foreach (string x in words)
{
wfile.Write(x);
wfile.Write(''t');
}
Console.WriteLine(''n');
}
}
}
file.Close();
wfile.Close();
// Suspend the screen.
Console.ReadLine();
}
将Console.WriteLine(''n');
更改为wfile.WriteLine(''n');
你可以用一种更好的方式来做到这一点:
var file1 = File.ReadLines(@"path1");
var file2 = File.ReadLines(@"path2");
var common = file1.Intersect(file2); //returns all lines common to both files
File.WriteAllLines("path3", common);