简体   繁体   English

比较两个文本文件

[英]comparing two text files

I have two text files bala.txt and bala1.txt 我有两个文本文件bala.txtbala1.txt

bala.txt contains text line by line as bala.txt逐行包含文本为

balamurugan,rajendran,chendurpandian
christopher
updateba

bala1.txt contains text line by line as bala1.txt逐行包含文本为

ba

Here i need to check bala1.txt with bala.txt and write into a log file as 在这里,我需要使用bala.txt检查bala1.txt并写入日志文件为

LineNo : 0 : balamurugan,rajendran,chendurpandian
LineNo : 2 : updateba

now its writing only one line as 现在它只写一行

LineNo : 0 : balamurugan,rajendran,chendurpandian

after that while loop is getting out 之后,while循环消失了

Here is my code 这是我的代码

while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null))
                        {
                            if (line.Contains(line2))
                            {
                                dest.WriteLine("LineNo : " + counter.ToString() + " : " + line + "<br />");
                            }
                            counter++;
                        }

any suggestion?? 有什么建议吗?

EDIT: 编辑:

string FilePath = txtBoxInput.Text;
    string Filepath2 = TextBox1.Text;
    int counter = 0;
    string line;
    string line2;


        DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
        var dir = @"D:\New folder\log";

        if (Folder.Exists)
        {
            if (!Directory.Exists(dir))
                Directory.CreateDirectory(dir);
        }
        if (File.Exists(FilePath) & File.Exists(Filepath2))
        {
            // Read the file and display it line by line.
            using (var file = File.OpenText(FilePath))
            using (var file2 = File.OpenText(Filepath2))
            using (var dest = File.AppendText(Path.Combine(dir, "log.txt")))
            {
                while (((line = file.ReadLine()) != null & (line2 = file2.ReadLine()) != null))
                {
                    if (line.Contains(line2))
                    {
                        dest.WriteLine("LineNo : " + counter.ToString() + " : " + line + "<br />");
                    }

                    counter++;
                }

            }

} }

EDIT(2): I need to create two text files into the folder log and write into that text files as ba.txt with contents as EDIT(2):我需要在文件夹日志中创建两个文本文件,并将其作为ba.txt写入文本文件,其内容为

LineNo : 0 : balamurugan,rajendran,chendurpandian
LineNo : 2 : updateba

and ra.txt with contents as ra.txt ,内容为

LineNo : 0 : balamurugan,rajendran,chendurpandian

Any Suggestion?? 有什么建议吗?

EDIT(3): I need to create a folder named Log through code and in that log folder ba.txt and ra.txt have to be created. EDIT(3):我需要创建一个名为Log through code的文件夹, ra.txt必须在该日志文件夹中创建ba.txtra.txt

The underlying stream for the second file reader is reaching the end of the stream and not being reset before the next iteration of the loop. 第二个文件读取器的基础流正在到达流的末尾,并且在循环的下一次迭代之前不会被重置。 You'll need to copy all the lines of each file into memory before comparing them. 在比较它们之前,您需要将每个文件的所有行复制到内存中。 Try this: 尝试这个:

List<string> file1Lines = new List<string>();
List<string> file2Lines = new List<string>();

while ((line = file.ReadLine()) != null)
{
    file1Lines.Add(line);
}

while ((line2 = file2.ReadLine()) != null)
{
    file2Lines.Add(line2);
}

foreach (string f1line in file1Lines)
{
    foreach (string f2line in file2Lines)
    {
        if (f1line.Contains(f2line))
        {
            dest.WriteLine("LineNo : " + counter.ToString() + " : " + f1line + "<br />");
        }

    }
    counter++;
}

try this: 尝试这个:

string FilePath = txtBoxInput.Text, Filepath2 = TextBox1.Text;
int counter = 0;
string line, line2;

DirectoryInfo Folder = new DirectoryInfo(textboxPath.Text);
var dir = @"D:\New folder\log";

if (Folder.Exists)
    if (!Directory.Exists(dir)) Directory.CreateDirectory(dir);

if (File.Exists(FilePath) && File.Exists(Filepath2))
{   // Read the file and display it line by line.
    using (var file = File.OpenText(FilePath))
    {
        using (var file2 = File.OpenText(Filepath2))
        {
            while((line2 = file2.ReadLine()) != null)
            {
                //YOU NEED TO CHECK IF FILE ALREADY EXISTS
                // AND YOU WANT TO OVERWRITE OR CREATE NEW
                //WITH SOME OTHER NAME
                //---------------------------------------CREATE NEW FILE FOR
                //---------------------------------------EACH LINE IN file2
                using (var dest = File.AppendText(Path.Combine(dir, line2 + ".txt")))
                {
                    while ((line = file.ReadLine()) != null)
                    {
                        if (line.Contains(line2))
                            dest.WriteLine("LineNo : " + 
                                counter.ToString() + " : " + line + "<br />");
                        counter++;
                    }
                    //IF THE SECOND FILE ONLY CONTAINS 1 LINE THEN YOU
                    //DON'T NEED THIS.
                    //we need to go to begning of first file
                    file.BaseStream.Seek(0, SeekOrigin.Begin);
                    counter = 0;
                }
            }
        }
    }
}

EDIT: to get file path from user. 编辑:从用户获取文件路径。

Give a button to open file dialog box to chose file or folder browser dialog if you want to get directory name to save log files. 如果要获取目录名称来保存日志文件,则提供一个“打开文件”对话框以选择文件或文件夹浏览器对话框。

//OPEN FILE -- you will need two buttons one 
//for each text boxes
void btnFile_Click(object sender, EventArgs e)
{
    var fbd = new OpenFileDialog();
    fbd.Multiselect = false;
    fbd.CheckFileExists = true;
    fbd.CheckPathExists = true;
    if(fbd.ShowDialog()==DialogResult.Ok)
    {
        textBox1.Text = fbd.FileName;
    }
}

//SELECT FOLDER
string _logFolderPath;//use this inplace of @"D:\new folder\log";
void btnFolder_click(object sender, EventArgs e)
{
    var fd = new FolderBrowserDialog();
    if(fd.ShowDialog()==DialogResult.OK)
    {
        _logFolderPath = fd.SelectedPath;
    }
}
private void Comparer(string fileLocation1, string fileLocation2, string resultLocation)
{
    StreamReader source = new StreamReader(fileLocation1);
    StreamReader pattern = new StreamReader(fileLocation2);
    StreamWriter result = File.CreateText(resultLocation);

    //reading patterns
    List<String> T = new List<string>();
    string line;
    while ((line = pattern.ReadLine()) != null)
        T.Add(line);
    pattern.Close();

    //finding matches and write them in output
    int counter = 0;
    while ((line = source.ReadLine()) != null)
    {
        foreach (string pat in T)
        {
            if (line.Contains(pat))
            {
                result.WriteLine("LineNo : " + counter.ToString() + " : " + line);
                break; //just if you want distinct output
            }
        }
        counter++;
    }

    source.Close();
    result.Close();
}

----------------------------EDIT---------------------------------- for the one you mentioned in comment - - - - - - - - - - - - - - 编辑 - - - - - - - - - - - -------------对于您在评论中提到的那个

private void Comparer(string fileLocation1, string fileLocation2, string resultFolder)
{
    StreamReader source = new StreamReader(fileLocation1);
    StreamReader pattern = new StreamReader(fileLocation2);
    Directory.CreateDirectory(resultFolder);
    //reading patterns
    List<String> T = new List<string>();
    string line;
    while ((line = pattern.ReadLine()) != null)
        T.Add(line);
    pattern.Close();

    //finding matches and write them in output
    int counter;
    foreach (string pat in T)
    {
        StreamWriter result = File.CreateText(Path.Combine(resultFolder, pat + ".txt"));
        source.BaseStream.Position = counter = 0;
        while ((line = source.ReadLine()) != null)
        {
            if (line.Contains(pat))
                result.WriteLine("LineNo : " + counter.ToString() + " : " + line);
            counter++;
        }
        result.Close();
    }
    source.Close();
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM