我有一个大约 2GB 的巨大文本文件,我试图用 C# 解析它。该文件具有用于行和列的自定义分隔符。我想解析文件并提取数据并通过插入列标题并用换行符替换 RowDelimiter 和按制表符替换 ColumnDelimiter 来将数据写入另一个文件,以便我可以以表格格式获取数据。
样本数据:
1'~'2'~'3#####11'~'12'~'13
行分隔符: 列 #####
分隔符: '~'
我继续在System.OutOfMemoryException下面的线路上
while ((line = rdr.ReadLine()) != null)
public void ParseFile(string inputfile,string outputfile,string header)
{
using (StreamReader rdr = new StreamReader(inputfile))
{
string line;
while ((line = rdr.ReadLine()) != null)
{
using (StreamWriter sw = new StreamWriter(outputfile))
{
//Write the Header row
sw.Write(header);
//parse the file
string[] rows = line.Split(new string[] { ParserConstants.RowSeparator },
StringSplitOptions.None);
foreach (string row in rows)
{
string[] columns …Run Code Online (Sandbox Code Playgroud) c# ×1