如何将字节数组列表写入文件c#

时间:2016-08-04 13:38:27

标签: c# arrays visual-studio download byte

我有一个上传器,我用它来分割文件并将它们上传到我的sql server。然后我下载每个块并创建一个临时文件。 我试图将字节数组(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文件,我应该获得100KB文件。

更新2:下面的代码更有意义,但现在我得到一个&#34;流不可写错误&#34;

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);
                    }
                }
            }
        } 

3 个答案:

答案 0 :(得分:2)

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

我不知道您的要求是什么,但根据您提供的代码,您可以在第一个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. 

                }
            }
        } 
     }

答案 1 :(得分:0)

如果我理解你想要什么:

// 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);
        }
    }
}

答案 2 :(得分:0)

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