在C#中合并2个文本文件 [英] Merging 2 Text Files in C#

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

问题描述

首先,我想提一提的是前几天我才开始学习C#,所以我的知识是有限的。



我是试图创建一个程序,将解析用户输入的某些短语的文本文件,然后将其输出到一个新的文本文件。



目前,我有它程序搜索原始输入文件并收集用户输入的选定文本,应对这些行,创建新的文本文件,然后将它们合并在一起,并在之后删除。

我猜这不是创建这个最有效的方式,但我只是创建它,并在一个逻辑庄园工作,为我理解为新手。



<代码如下;

  private void TextInput1()
{
using(StreamReader fileOpen = new StreamReader(txtInput.Text))
{
using(StreamWriter fileWrite = new StreamWriter(@* DIRECTORY * \FIRSTFILE.txt))
{
字符串文件; ((file = fileOpen.ReadLine())!= null)
if(file.Contains(txtFind.Text))
{
fileWrite。写(文件+\r\\\
);





$ b private void TextInput2()
{
using(StreamReader fileOpen = new StreamReader(txtInput.Text))
{
using(StreamWriter fileWrite = new StreamWriter(@* DIRECTORY * \SECONDFILE.txt))
{
string文件; ((file = fileOpen.ReadLine())!= null)
if(file.Contains(txtFind2.Text))
{
fileWrite。写(\r\\\
+文件);





$ b private static void Combination()
{
ArrayList fileArray = new ArrayList(); (StreamReader reader = File.CreateText(@* DIRECTORY * \FINALOUTPUT.txt))
{

using(StreamReader reader = OpenText(@* DIRECTORY * \FIRSTFILE.txt))
{
writer.Write(reader.ReadToEnd());


using(StreamReader reader = File.OpenText(@* DIRECTORY * \SECONDFILE.txt))
{
writer.Write(reader。 ReadToEnd的());




$ b private static void Delete()
{
if(File.Exists(@ * DIRECTORY * \FIRSTFILE.txt))
{
File.Delete(@* DIRECTORY * \FIRSTFILE.txt); (@* DIRECTORY * \SECONDFILE.txt))
{
File.Delete(@* DIRECTORY * \SECONDFILE.txt);





正在创建的输出文件只是输出第一个文本输入,然后是第二个输入。我想知道是否有可能能够将它们合并为1个文件,每次1行,因为它是一个连续的文件,意味着需要来自输入1的信息,其次是2,而不是全部1,然后全部是2。 / p>

感谢Neil。

解决方案

一行一行的合并文件,你可以用你的Combination()代码替换这个

  string [] file1 = File.ReadAllLines * DIRECTORY * \FIRSTFILE.txt); 
string [] file2 = File.ReadAllLines(* DIRECTORY * \SECONDFILE.txt);

using(StreamWriter writer = File.CreateText(@* DIRECTORY * \FINALOUTPUT.txt))
{
int lineNum = 0;
while(lineNum< file1.Length || lineNum< file2.Length)
{
if(lineNum< file1.Length)
writer.WriteLine(file1 [lineNum ]);
if(lineNum< file2.Length)
writer.WriteLine(file2 [lineNum]);
lineNum ++;






这假定两个文件不包含相同的行数。

Firstly, i'd just like to mention that I've only started learning C# a few days ago so my knowledge of it is limited.

I'm trying to create a program that will parse text files for certain phrases input by the user and then output them into a new text document.

At the moment, i have it the program searching the original input file and gathering the selected text input by the user, coping those lines out, creating new text files and then merging them together and also deleting them afterwards.

I'm guessing that this is not the most efficient way of creating this but i just created it and had it work in a logical manor for me to understand as a novice.

The code is as follows;

private void TextInput1()
    {
        using (StreamReader fileOpen = new StreamReader(txtInput.Text))
        {
            using (StreamWriter fileWrite = new StreamWriter(@"*DIRECTORY*\FIRSTFILE.txt"))
            {
                string file;
                while ((file = fileOpen.ReadLine()) != null)
                {
                    if (file.Contains(txtFind.Text))
                    {
                            fileWrite.Write(file + "\r\n");
                    }
                }
            }
        }
    }

    private void TextInput2()
    {
        using (StreamReader fileOpen = new StreamReader(txtInput.Text))
        {
            using (StreamWriter fileWrite = new StreamWriter(@"*DIRECTORY*\SECONDFILE.txt"))
            {
                string file;
                while ((file = fileOpen.ReadLine()) != null)
                {
                    if (file.Contains(txtFind2.Text))
                    {
                        fileWrite.Write("\r\n" + file);
                    }
                }
            }
        }
    }

    private static void Combination()
    {
        ArrayList fileArray = new ArrayList();

        using (StreamWriter writer = File.CreateText(@"*DIRECTORY*\FINALOUTPUT.txt"))
        {

            using (StreamReader reader = File.OpenText(@"*DIRECTORY*\FIRSTFILE.txt"))
            {
                writer.Write(reader.ReadToEnd());
            }

            using (StreamReader reader = File.OpenText(@"*DIRECTORY*\SECONDFILE.txt"))
            {
                writer.Write(reader.ReadToEnd());
            }
        }

    }

    private static void Delete()
    {
        if (File.Exists(@"*DIRECTORY*\FIRSTFILE.txt"))
            {
                File.Delete(@"*DIRECTORY*\FIRSTFILE.txt");
            }

        if (File.Exists(@"*DIRECTORY*\SECONDFILE.txt"))
            {
                 File.Delete(@"*DIRECTORY*\SECONDFILE.txt");
            }

    }

The output file that is being created is simply outputting the first text input followed by the second. I am wondering if it is possible to be able to merge them into 1 file, 1 line at a time as it is a consecutive file meaning have the information from Input 1 followed 2 is needed rather than all of 1 then all of 2.

Thanks, Neil.

解决方案

To combine the two files content in an one merged file line by line you could substitute your Combination() code with this

string[] file1 = File.ReadAllLines("*DIRECTORY*\FIRSTFILE.txt");
string[] file2 = File.ReadAllLines("*DIRECTORY*\SECONDFILE.txt");

using (StreamWriter writer = File.CreateText(@"*DIRECTORY*\FINALOUTPUT.txt"))
{
    int lineNum = 0;
    while(lineNum < file1.Length || lineNum < file2.Length)
    {
        if(lineNum < file1.Length)
            writer.WriteLine(file1[lineNum]);
        if(lineNum < file2.Length)
            writer.WriteLine(file2[lineNum]);
        lineNum++;
    }
}

This assumes that the two files don't contains the same number of lines.

这篇关于在C#中合并2个文本文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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