比较两个文本文件 [英] comparing two text files

查看:85
本文介绍了比较两个文本文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个文本文件 bala.txt bala1.txt

bala.txt包含行的文本行

bala.txt contains text line by line as

balamurugan,rajendran,chendurpandian
christopher
updateba

bala1.txt包含行的文本行

bala1.txt contains text line by line as

ba

在这里,我需要检查bala1.txt与bala.txt并写入到一个日志文件

Here i need to check bala1.txt with bala.txt and write into a log file as

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

现在的写作只有一个行

LineNo : 0 : balamurugan,rajendran,chendurpandian

之后while循环失控

after that while loop is getting out

下面是我的code

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

任何建议??

编辑:

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++;
                }

            }

}

编辑(2):
  我需要创建两个文本文件到该文件夹​​的日志,写成文本文件作为 ba.txt 与内容

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

ra.txt 与内容

LineNo : 0 : balamurugan,rajendran,chendurpandian

任何建议??

编辑(3):
 我需要创建一个名为登录通过code和在日志文件夹 ba.txt 文件夹和 ra.txt 已被创建。

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.

推荐答案

试试这个:

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;
                }
            }
        }
    }
}

编辑:从用户获取文件路径

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;
    }
}

这篇关于比较两个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆