在读取 CSV 文件时替换值并保存文件 (C#)

Replace value & save file during reading CSV file (C#)

我正在读取 csv 文件:

string line;

StreamReader sr = new StreamReader(file.ToString());

while ((line = sr.ReadLine()) != null)
{
    string col1 = line.Split(',')[10]; //old value
    col1 = "my value"; //new value
}

sr.Close();
sr.Dispose();

我想用新值替换旧值。

然后我需要保存更改后的文件。

我该怎么做?

我建议使用 File class 而不是 StreamReaderLinq查询数据的时候很方便:

var modifiedData = File
  .ReadLines(file.ToString())
  .Select(line => line.Split(',')) 
  .Select(items => {
     //TODO: put relevant logic here: given items we should return csv line
     items[10] = "my value";

     return string.Join(",", items);
   })
  .ToList(); // <- we have to store modified data in memory

File.WriteAllLines(file.ToString(), modifiedData);

另一种可能性(例如,当初始文件太长而无法容纳内存时)是将修改后的数据保存到临时文件中,然后Move它:

 var modifiedData = File
  .ReadLines(file.ToString())
  .Select(line => line.Split(',')) 
  .Select(items => {
     //TODO: put relevant logic here: given items we should return csv line
     items[10] = "my value";

     return string.Join(",", items);
   });

 string tempFile = Path.Combine(Path.GetTempPath(), $"{Guid.NewGuid()}.tmp");

 File.WriteAllLines(tempFile, modifiedData);

 File.Delete(file.ToString());
 File.Move(tempFile, file.ToString());

一次读取整个文件是memory-expensive。更不用说创建它的平行副本了。使用流可以修复它。试试这个:

void Modify()
{
    using (var fs = new FileStream(file, FileMode.Open, FileAccess.ReadWrite))
    {
        string line;
        long position;

        while ((line = fs.ReadLine(out position)) != null)
        {
            var tmp = line.Split(',');
            tmp[1] = "00"; // new value
            var newLine = string.Join(",", tmp);
            fs.WriteLine(position, newLine);
        }
    }
}

扩展名:

static class FileStreamExtensions
{
    private static readonly char[] newLine = Environment.NewLine.ToCharArray();
    private static readonly int length = Environment.NewLine.Length;
    private static readonly char eof = '\uFFFF';

    public static string ReadLine(this FileStream fs, out long position)
    {
        position = fs.Position;
        var chars = new List<char>();
        char c;
        while ((c = (char)fs.ReadByte()) != eof && (chars.Count < length || !chars.Skip(chars.Count - 2).SequenceEqual(newLine)))
        {
            chars.Add(c);
        }
        fs.Position--;

        if (chars.Count == 0)
            return null;

        return new string(chars.ToArray());
    }

    public static void WriteLine(this FileStream fs, long position, string line)
    {
        var bytes = line.ToCharArray().Concat(newLine).Select(c => (byte)c).ToArray();
        fs.Position = position;
        fs.Write(bytes, 0, bytes.Length);
    }
}

缺点是您必须保持值的长度相同。例如。 999__9 的长度都是 3。解决这个问题会使事情变得更加复杂,所以我就这样吧。

Full working example