合并文本文件 [英] Merging text files together

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

问题描述



我有以下代码和许多文本文件.我希望将所有文件合并为1,只需将它们加在一起即可. I.E文件1就像

absdc
eiojs
sjkdi

文件2就像
09asd
akjsd
341u0

输出将是:

absdc
eiojs
sjkdi

09asd
akjsd
341u0


我在尝试执行此操作的地方有以下函数,但是我的字符串不够长:(我应该如何做更好的方法?

Hi,

I have the following code, and a lot of text files. I want all files to be combined in 1, simply by adding them together. I.E file 1 is like

absdc
eiojs
sjkdi

file 2 is like
09asd
akjsd
341u0

the output will be:

absdc
eiojs
sjkdi

09asd
akjsd
341u0


I have the following function where I try to do this, but my string is not long enough :( How should I do this a better way?

string fullFile;
private void Merge()
{
    System.IO.StreamWriter data = new System.IO.StreamWriter(@"c:\" + Somevariable + @"\" + Somevariable + ".txt");
    for (int i = 1; i < 152; i++)
    {
        try
        {
            string[] fullfile = System.IO.File.ReadAllLines(@"c:\" + Somevariable+ @"\h" + i + ".7.txt");
            int length = fullfile.Length;
            for (int j = 0; j <= length; j++)
            {
                fullFile = fullFile+fullfile[j] + "\n"; // this string
            }
        }
        catch { i = 153; }
    }
    data.WriteLine(fullFile);
    data.Close();
}

推荐答案

我建​​议尝试此列表中的第二个:

http://stackoverflow.com/questions/6311358/ficient-way-to-组合多个文本文件 [ ^ ]
http://www.c-sharpcorner.com/uploadfile/sonuraj /merge-two-files-with-C-Sharp/ [ http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/0faebd8f-339b-4f74-96e0-d4a71634ade0 [
I would recommend trying the the second one in this list:

http://stackoverflow.com/questions/6311358/efficient-way-to-combine-multiple-text-files[^]
http://www.c-sharpcorner.com/uploadfile/sonuraj/merge-two-files-with-C-Sharp/[^]
http://social.msdn.microsoft.com/Forums/eu/csharpgeneral/thread/0faebd8f-339b-4f74-96e0-d4a71634ade0[^]

Your code assumes that the files are text files, which may be OK, but is not necessarily true. Of course could have problems with using bytes in that you have text files, but they are different encodings.


您也可以使用字符串来实现.如果您使用的是文件,则只需将两者连接即可.
You can achieve this using strings also. If you are using file then just concatenate both to join.


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

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