读取 Stream 中的多个文件
嘿!
如何一次读取多个文本文件? 我想要做的是读取一系列文件并将它们全部附加到一个大文件中。目前我正在这样做:
- 获取每个文件并使用 StreamReader 打开它
- 在 StringBuilder 中完全读取 StreamReader 并将其附加到当前 StreamBuilder
- 检查是否超出内存大小,如果超出则将 StringBuilder 写入文件末尾并清空 不幸的是, StrigBuilder 的
读取速度平均只有 4MB/秒。我注意到,当我在磁盘上移动文件时,速度为 40 MB/秒。 我正在考虑缓冲流中的文件并像写入一样一次性读取它们。知道我怎样才能实现这个目标吗?
更新:
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();
}
这就是我现在正在做的。
Hei!
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:
- take each file and open it with a StreamReader
- read the StreamReader completely in a StringBuilder and append it to the current StreamBuilder
- check if the memory size is exceeded and if yes write the StringBuilder at the end of the file and empty the StrigBuilder
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?
Update:
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();
}
This is how I'm doing it now.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
一方面,您需要区分流(二进制数据)和
StreamReader
或更一般的TextReader
(文本数据)。听起来您想创建一个
TextReader
的子类,它将接受(在其构造函数中)一堆TextReader
参数。您不需要在这里急切地阅读任何内容...但是在您重写的Read
方法中,您应该从“当前”阅读器中读取内容,直到读完为止,然后从下一个开始。请记住,Read
没有有来填充它所给出的缓冲区 - 所以你可以这样做:我强烈怀疑已经有第三方库可以做到这一点有点解复用,请注意...
For one thing, you need to differentiate between streams (binary data) and
StreamReader
s or more generallyTextReader
s (text data).It sounds like you want to create a subclass of
TextReader
which will accept (in its constructor) a bunch ofTextReader
parameters. You don't need to eagerly read anything here... but in theRead
methods that you override, you should read from "the current" reader until that's exhausted, then start on the next one. Bear in mind thatRead
doesn't have to fill the buffer it's been given - so you could do something like:I strongly suspect there are already third party libraries to do this sort of demuxing, mind you...
如果您所做的只是读取文件,然后将它们连接到磁盘上的新文件,那么您可能根本不需要编写代码。使用 Windows 复制命令:
如果需要,您可以通过 Process.Start 调用此命令。
当然,这是假设您没有对文件或其内容执行任何自定义逻辑。
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:
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.
这应该很快(但它会将整个文件加载到内存中,因此可能无法满足所有需求):
This should be fast (but it'll load the entire files in memory, so might not fit with every need):