Failed to write large amount of data to stream

前端 未结 2 654
甜味超标
甜味超标 2021-01-03 05:09

When I\'m trying to write very large amount of data (list with 300 000 rows and more) to memory stream using CsvHelper, it throws the exception \"System.IO.IOExcepti

2条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 05:52

    Many thanks Spender, like he mentioned in the comment below the question, it has been fixed by replacing MemoryStream with FileStream and writing data direct into the file.

    It was absolutely useless in my case to write data to MemoryStream and then copy it again into the file without any reason. Thanks him again for opening my eyes on that fact.

    My fixed code below.

    using (var fileStream = File.Create(path))
    {
        using (var streamWriter = new StreamWriter(fileStream, encoding ?? Encoding.ASCII))
        {
            var csvWriter = new CsvWriter(streamWriter, GetConfiguration(delimiter, mappingClassType, mappingActions));
            csvWriter.WriteRecords(data);
        }
    }
    

    Now it works with any amount of input data.

提交回复
热议问题