附加新文本文件中所有.txt文件的内容。 [英] Append the content from all the .txt files in the new text file.

查看:91
本文介绍了附加新文本文件中所有.txt文件的内容。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

搜索给定路径中的所有.txt文件

创建一个新的文件文本文件。

在新文本中附加所有.txt文件的内容文件。







i让我的第一个任务搜索给定路径中的所有文本但无法追加他们在新文件夹...任何想法..

Search for all the .txt files in the given path
Create a new file text file.
Append the content from all the .txt files in the new text file.



i have my 1st task search all text from a given path but not able to append them in new folder...any idea..

推荐答案

我认为这两个链接会对你有所帮助。但要记住一件事,组合大小您的其他文本文件,可能经常导致memoryOutExceptions.Good运气:)

http://stackoverflow.com/questions/6311358/efficient-way-to-combine-multiple-text-files [ ^ ]

http://stackoverflow.com/问题/ 6397645 / c-sharp-appending-text-files [ ^ ]
I think these 2 links will help you a lot.But keep one thing in mind, the combined size of your other text files which may often cause memoryOutExceptions.Good luck :)
http://stackoverflow.com/questions/6311358/efficient-way-to-combine-multiple-text-files[^]
http://stackoverflow.com/questions/6397645/c-sharp-appending-text-files[^]


如果您在目录中搜索过,则将文件名放入列表< string> fileNames。

使用 foreach 循环在列表中迭代。



然后在foreach循环中使用以下基本文件复制(附加模式)代码。



If you have searched in the directory, Then put the file names into a List<string> fileNames.
Iterate in the list using foreach loop.

Then use the following basic file copying (in append mode) code inside that foreach loop.

using (Stream input = File.OpenRead(fileName))
using (Stream output = new FileStream("newFile.txt", FileMode.Append,
                                      FileAccess.Write, FileShare.None))
{
    input.CopyTo(output);
}
File.Delete("fileName.txt");





请找到copyto 方法以下。





Please find the copyto method below.

public static class StreamExtensions
{
    public static void CopyTo(this Stream input, Stream output)
    {
        if (input == null)
        {
            throw new ArgumentNullException("input)";
        }
        if (output == null)
        {
            throw new ArgumentNullException("output)";
        }
        byte[] buffer = new byte[8192];
        int bytesRead;
        while ((bytesRead = input.Read(buffer, 0, buffer.Length)) > 0)
        {
            output.Write(buffer, 0, bytesRead);
        }
    }
}





希望我已经回答了你的问题。 :)



Hope i have answered your question. :)


试试这个:
String[] txtfiles = System.IO.Directory.GetFiles(ThePathToYourDir, "*.txt", System.IO.SearchOption.AllDirectories);

using (Stream output = new FileStream("YourCongregatedTextFile.txt", FileMode.Append,
                                      FileAccess.Write, FileShare.None))
{
    foreach (string file in txtfiles)
    {
       using (Stream input = File.OpenRead(file))
       {
           input.CopyTo(output); 
       }
    }
}





(我在解决方案文本框中将其写成纯文本,希望没有错别字......)



干杯,

Edo



(I wrote it as plain text right on the solution textbox, hope there are no typos...)

Cheers,
Edo


这篇关于附加新文本文件中所有.txt文件的内容。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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