简体   繁体   English

在 C# 中将字节流写入文件的有效方法是什么?

[英]What is the efficient way to write stream of bytes into a file in c#?

I have big big data in form of bytes around 5GB .我有大约5GB字节形式的大数据。

I need to store this data in a file ServerData.xml .我需要将此数据存储在文件ServerData.xml This data should be first converted into string and then should be saved to file so that we can perform operation on the file.这个数据应该先转换成字符串,然后保存到文件中,这样我们就可以对文件进行操作了。

I used below code to convert stream of bytes to string and then to save the same in a file.我使用下面的代码将字节流转换为字符串,然后将其保存在文件中。

private const string fileName = "ServerData.xml";

public void ProcessBuffer(byte[] receiveBuffer, int bytes)
{
    if (!File.Exists(fileName))
    {
        using (File.Create(fileName)) { };
    }

    TextWriter tw = new StreamWriter(fileName, true);
    tw.Write(Encoding.UTF8.GetString(receiveBuffer).TrimEnd((Char)0));
    tw.Close();
}

Is it the right way ?这是正确的方法吗?

or please suggest better way so that there should not be any memory issue if any in future ?或者请提出更好的方法,以便将来不会出现任何内存问题?

You can simply write these bytes to a file using FileStream :您可以简单地使用FileStream将这些字节写入文件:

public void ProcessBuffer(byte[] receivedBuffer, int bytes)
{
    using (var fileStream = new FileStream(fileName, FileMode.Create)) // overwrites file
    {
        fileStream.Write(receivedBuffer, 0, bytes);
    }
}

Update: You won't be able to work with such a big XML document if you don't have enough resources.更新:如果您没有足够的资源,您将无法处理这么大的 XML 文档。 I would suggest reformatting this file.我建议重新格式化这个文件。 For example, I would parse this XML and insert data into a SQL database.例如,我会解析这个 XML 并将数据插入到 SQL 数据库中。 Then, you can easily operate with such amounts of data.然后,您可以轻松地处理如此大量的数据。

The code in your question can only work if ProcessBuffer is always called with a UTF-8 encoded text that is broken on code point boundaries.只有在始终使用在代码点边界上损坏的 UTF-8 编码文本调用ProcessBuffer ,您问题中的代码才能工作。 That seems pretty unlikely to me, so I would expect that you encounter errors when decoding to text.这对我来说似乎不太可能,所以我希望您在解码为文本时会遇到错误。

However, decoding to text and then writing, is rather pointless and indeed counter-productive.然而,解码为文本然后写作,是相当没有意义的,甚至适得其反。 The bytes are already UTF-8 encoded.字节已经是 UTF-8 编码的。 Write them directly to file as they arrive from the socket.当它们从套接字到达时,将它们直接写入文件。 Don't perform any processing of them.不要对它们进行任何处理。 When you come to read the XML using XmlReader , the parser will read the encoding as UTF-8 from the document's XML declaration, and be able to decode the rest of the document.当您使用XmlReader读取 XML 时,解析器将从文档的 XML 声明中读取编码为 UTF-8,并能够解码文档的其余部分。 I am assuming that the document's XML declaration specifies UTF-8 but that seems highly likely.我假设文档的 XML 声明指定了 UTF-8,但这似乎很有可能。 You should check.你应该检查一下。

You should get rid of the text writer which is no use to you for writing bytes.你应该摆脱对你写字节没有用的文本编写器。 Write the bytes directly to a file stream.将字节直接写入文件流。 And try to avoid opening and closing the file repeatedly.并尽量避免反复打开和关闭文件。 That's very inefficient.这是非常低效的。 Open and close the file exactly once.打开和关闭文件一次。

Why do you need to convert it to a string?为什么需要将其转换为字符串?

using System.IO;

public static void WriteBytes(byte[] bytes, string filename)
{
    using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))
    using (BinaryWriter writer = new BinaryWriter(fs, Encoding.UTF8))
    {
        writer.Write(bytes);
    }
}

I would prefer that I write all bytes to file.我更愿意将所有字节写入文件。 And when reading, convert it to string and then convert to XML using XDocument, XElement etc. By writing bytes in file you will save space, and it is efficient,读取时,将其转换为字符串,然后使用 XDocument、XElement 等转换为 XML。通过在文件中写入字节可以节省空间,而且效率很高,

Instead of using FileStream, I will prefer File.WriteAllBytes method.我更喜欢 File.WriteAllBytes 方法,而不是使用 FileStream。

private const string fileName = "ServerData.xml";
public void ProcessBuffer(byte[] receiveBuffer, int bytes)
{
    File.WriteAllBytes(filename, bytes);


    // And when reading
    var bytes = File.ReadAllBytes(filename);
    var binaryReader = new BinaryReader(new MemoryStream(bytes));
    // Parse strings and make xml,
    binaryReader.ReadString();

}

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

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