简体   繁体   English

在C#中联接两个文件,拆分选项

[英]Joining two files in C#, split options

I am currently trying to work on files, joining multiple of them and having problem because the last work from file 1 is linked with first word from file 2. For example: 我目前正在尝试处理文件,将它们加入多个文件并遇到问题,因为文件1的最后一个工作与文件2的第一个单词相关联。例如:

File 1:John has got new haircut 文件1:约翰有新发型

File 2: Mike has got new haircut 文件2:迈克有新发型

and it prints me "haircutMike". 它打印我“ haircutMike”。

The code I am using to split words: 我用来拆分单词的代码:

        input.Split(' ').ToList().ForEach(n =>{});

I am also making one big file from multiple ones like so: 我还从多个文件中制作一个大文件,如下所示:

string[] files = { "f1.txt", "f2.txt" };
        FileStream outputFile = new FileStream("new.txt", FileMode.Create);

        using (StreamWriter ws = new StreamWriter(outputFile))
        {
            foreach (string file in files)
            {
                ws.Write(System.IO.File.ReadAllText(file) + " ");
            }
        }

@EDIT @编辑

Changed some code, of course I meant to use stream not binary,also I am using split because I want to count the number of each word in files so I have to split spaces, dots etc. 更改了一些代码,当然我的意思是使用流而不是二进制,我也使用了split,因为我想计算文件中每个单词的数量,所以我必须拆分空格,点等。

You mentioned to use + " " option, although it works, but it added me 1 letter to the total count. 您提到使用+“”选项,虽然它可以工作,但是它在总数上给了我1个字母。

EDIT: for multiple input files: 编辑:对于多个输入文件:

 string[] files = { "f1.txt", "f2.txt" };

 var allLines = files.SelectMany(i => System.IO.File.ReadAllLines(i));

 System.IO.File.WriteAllLines("new.txt", allLines.ToArray());

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

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