【问题标题】:How to write a List of Byte Arrays to a file c#如何将字节数组列表写入文件c#
【发布时间】:2016-08-04 13:38:27
【问题描述】:

我有一个上传器,用于拆分文件并将它们上传到我的 sql 服务器。然后我下载每个块并创建一个临时文件。 我正在尝试将字节数组(byte [])列表写入一个文件以重新创建该文件。这是因为当我尝试读取字节数组列表时 进入一个数组,我得到一个 OutOfMemory 异常。我只是想知道最好的方法是什么。谢谢!

 string path = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
        int currentRowSelection = fUS_FileDataGridView.CurrentCell.RowIndex;
        var totalNumber = fUS_FileDataGridView.Rows[currentRowSelection].Cells[6].Value;
        for (int i = 1; i < 149; i++)
        {
            using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream1))
                {
                    list_.Add(reader.ReadBytes((int)stream1.Length));
                    stream1.Close();
                    stream1.Dispose();
                    reader.Close();
                    reader.Dispose();

                }
            }
        }
        //array_ = list_.SelectMany(a => a).ToArray();

        filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
        foreach (byte[] bytes in list_)
        {
            var doc = System.Text.Encoding.Default.GetString(bytes);
            string textToAdd1 = bytes.ToString();
            try
            {
                using (FileStream fs = File.Create(filePaths_))
                using (StreamWriter writer = new StreamWriter(fs, Encoding.Default, 512))
                {
                    writer.Write(textToAdd1);
                    writer.Close();
                    writer.Dispose();
                }
            }
            finally
            {
            }
        }
    }

更新:我的问题与我发现的其他问题不同,因为我无法将字节数组列表放入单个数组中来写入文件。我目前只从我的代码中得到一个 1 KB 的文件,而我应该得到一个 100 KB 的文件。

更新 2:下面的代码更有意义,但现在我收到“流不可写错误”

filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
        using (FileStream fs = File.Create(filePaths_))
        for (int i = 0; i < 151; i++)
        {
            using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream1))
                {
                    using (StreamWriter writer = new StreamWriter(fs, Encoding.Default, 512))
                    {
                        writer.Write(reader);
                    }
                }
            }
        } 

【问题讨论】:

标签: c# arrays visual-studio download byte


【解决方案1】:

如果问题是内存不足,那么您应该考虑如何减少正在使用的内存量。

我不知道您的要求是什么,但根据您提供的代码,您可以在第一个 foreach 循环中完成所有编写工作。这样你一次只加载一个文件,一旦你处理完每个文件,GC就会释放内存。

    using (FileStream fs = File.AppendText(filePaths_)) 
    {
        for (int i = 1; i < 149; i++)
        {
            using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
            {
                using (var reader = new BinaryReader(stream1))
                {
                    //list_.Add(reader.ReadBytes((int)stream1.Length));
                    //Instead of adding that to list, write them to disk here
                    //fs.Write(...)
                    //...


                    stream1.Close();//No need for this, using is going to call it. 
                    stream1.Dispose();//No need for this, using is going to call it. 
                    reader.Close();//No need for this, using is going to call it. 
                    reader.Dispose();//No need for this, using is going to call it. 

                }
            }
        } 
     }

【讨论】:

  • 所有文件合并为一个文件,using (FileStream fs = File.Create(filePaths_)) 应该在for 循环之外。
  • 我使用了上面的代码并没有出现内存不足异常,但我只有一个 1 KB 的文件(应该更大)。我会再调查一下(已经盯着这个看了三天),然后回复你们。谢谢!
  • @TWelles1 你得到了 1kb 的文件,因为你使用的是 bytes[].ToString()。您的 doc 变量中已经有了字节数组的“真实”字符串值,您应该使用它
【解决方案2】:

如果我正确理解你想要什么:

// Fill list_
List<byte[]> list_ = null;
// ....

string filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
// Create FileStream and BinaryWriter
using (var fs = File.OpenWrite(filePaths_)){
    using (var bw = new BinaryWriter(fs)){
        foreach (var bytes in list_)
            bw.Write(bytes); // Write each byte array to the stream
    }
}

已更新。您可以采用更好的方式,直接将一个流复制到另一个流:

string filePaths_ = @"C:\Users\ATLAS\Desktop\13\fun.zip";
// Result FileStream and BinaryWriter
using (var fs = File.OpenWrite(filePaths_))
{
    for (int i = 1; i < 149; i++)
    {
        using (var stream1 = new FileStream(path + @"\" + i + ".zip", FileMode.Open, FileAccess.Read))
        {
            // Just copy stream1 to fs
            stream1.CopyTo(fs);
        }
    }
}

【讨论】:

    【解决方案3】:

    听起来您实际上想要做的是将每个字节数组附加到现有文件中。所以我认为,当您想将每个单独的数组写入文件时,您只需使用FileMode.Append(而不是 FileMode.Open)打开文件流。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-04-10
      • 2016-07-05
      • 2022-01-10
      • 2019-02-07
      • 2013-07-19
      • 1970-01-01
      • 1970-01-01
      • 2010-12-18
      相关资源
      最近更新 更多