读取流中的多个文件 [英] Reading multiple files in a Stream

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

问题描述

喜!

我如何读取多个文本文件一次? 我想要做的是阅读一系列文件,并追加他们都一个大文件。 Curently我这样做:

How can I read multiple text files at once? What I want to do is read a series of files and append all of them to one big file. Curently I am doing this:

  1. 在采取每一个文件,并与一个StreamReader打开
  2. 完全读取的StreamReader在一个StringBuilder,并把它添加到当前StreamBuilder
  3. 检查存储器大小被超过,如果是写的StringBuilder在文件的结尾并清空StrigBuilder

不幸的是,我观察到的读取速度平均仅为4MB /秒。我注意到,当我移动硬盘周围的文件,我得到40 MB /秒的速度。 我想缓存在流中的文件和读取一次,因为我做的,书写这一切。任何想法我怎么能做到这一点?

Unfortunately, I observed that the reading speed avg is only 4MB/sec. I noticed that when I move files around the disk I get a speed of 40 MB/sec. I am thinking of buffering the files in a Stream and reading them all at once as I do with the writting. Any idea how can I achieve this?

更新:

 foreach (string file in System.IO.Directory.GetFiles(InputPath))
        {
            using (StreamReader sr = new StreamReader(file))
            {

                try
                {
                    txt = txt+(file + "|" + sr.ReadToEnd());
                }
                catch // out of memory exception 
                {
                    WriteString(outputPath + "\\" + textBox3.Text, ref txt);
                    //sb = new StringBuilder(file + "|" + sr.ReadToEnd());
                    txt = file + "|" + sr.ReadToEnd();
                }

            }

            Application.DoEvents();
        }

这是我现在怎么做。

推荐答案

如果你正在做的是阅读文件,然后串联在一起到磁盘上的一个新的文件,你可能不需要写code在所有。使用Windows复制命令:

If all you're doing is reading files and then concatenating them together to a new file on disk, you might not need to write code at all. Use the Windows copy command:

C:\> copy a.txt+b.txt+c.txt+d.txt output.txt

您可以通过的Process.Start 如果你想叫这个。

You can call this via Process.Start if you want.

这,当然,假设你没有做任何自定义逻辑上的文件或其内容。

This, of course, assumes that you're not doing any custom logic on the files or their content.

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

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