如何将字节数组列表写入文件c#
本文关键字:文件 列表 数组 字节 字节数 | 更新日期: 2023-09-27 18:00:08
我有一个上传器,用来分割文件并将它们上传到我的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的文件,而我应该得到一个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);
}
}
}
}
如果问题是内存不足,那么您应该考虑如何减少使用的内存量。
我不知道你的要求是什么,但根据你提供的代码,你可以在第一个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.
}
}
}
}
如果我能正确理解你想要什么:
// 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);
}
}
}
听起来您实际上想要做的是将每个字节数组附加到现有文件中。因此,我认为,当您想将每个单独的数组写入文件时,只需使用FileMode.Append(而不是FileMode.open)打开文件流即可。