StreamReader的到文件? [英] StreamReader to file?

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

问题描述

我有一个输入流包装在一个就是System.IO.StreamReader ......我希望写流的一个文件(即StreamWriter的)内容

I have an input stream wrapped in a System.IO.StreamReader... I wish to write the content of the stream to a file (i.e. StreamWriter).

InputStream的长度是未知的。可能是几个字节和千兆字节的长度。

The length of the inputstream is unknown. Could be a few bytes and to gigabytes in length.

这是如何做到的最简单,不占用太多的内存?

How is this done the easiest that does not take up too much memory?

推荐答案

事情是这样的:

public static void CopyText(TextReader input, TextWriter output)
{
    char[] buffer = new char[8192];
    int length;
    while ((length = input.Read(buffer, 0, buffer.Length)) > 0)
    {
        output.Write(buffer, 0, length);
    }
}

请注意,这是非常相似,你会写什么到一个流中的内容复制到另一个 - 这恰好是二进制数据的文本数据,而不是

Note that this is very similar to what you'd write to copy the contents of one stream to another - this just happens to be text data instead of binary data.

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

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