通过C#编辑文本文件

我有一个巨大的文本文件,大小> 4GB,我想以编程方式替换它中的一些文本。 我知道我必须替换文本的行号,但问题是我不想将所有文本(以及我替换的行)复制到第二个文件。 我必须在源文件中执行此操作。 有没有办法在C#中做到这一点?

必须替换的文本与源文本的大小完全相同(如果这有帮助)。

由于文件太大,您可能需要查看.NET 4.0对内存映射文件的支持。 基本上,您需要将文件/流指针移动到文件中的位置,覆盖该位置,然后将文件刷新到磁盘。 您不需要将整个文件加载到内存中。

例如,在不使用内存映射文件的情况下,以下内容将覆盖ascii文件的一部分。 Args是输入文件,基于零的起始索引和新文本。

static void Main(string[] args) { string inputFilename = args[0]; int startIndex = int.Parse(args[1]); string newText = args[2]; using (FileStream fs = new FileStream(inputFilename, FileMode.Open, FileAccess.Write)) { fs.Position = startIndex; byte[] newTextBytes = Encoding.ASCII.GetBytes(newText); fs.Write(newTextBytes, 0, newTextBytes.Length); } } 

除非新文本与旧文本的大小完全相同,否则您必须重新编写该文件。 没有其他办法了。 您至少可以在不将整个文件保留在内存中的情况下执行此操作。

你好我测试了以下工作。这迎合了由Environment.NewLine分隔的可变长度行。 如果你有固定长度的线,你可以直接寻找它。为了将字节转换为字符串,反之亦然,你可以使用编码。

  static byte[] ReadNextLine(FileStream fs) { byte[] nl = new byte[] {(byte) Environment.NewLine[0],(byte) Environment.NewLine[1] }; List ll = new List(); bool lineFound = false; while (!lineFound) { byte b = (byte)fs.ReadByte(); if ((int)b == -1) break; ll.Add(b); if (b == nl[0]){ b = (byte)fs.ReadByte(); ll.Add(b); if (b == nl[1]) lineFound = true; } } return ll.Count ==0?null: ll.ToArray(); } static void Main(string[] args) { using (FileStream fs = new FileStream(@"c:\70-528\junk.txt", FileMode.Open, FileAccess.ReadWrite)) { int replaceLine=1231; byte[] b = null; int lineCount=1; while (lineCount(char)x).ToArray()); line = line.Replace("Text1", "Text2"); b=line.ToCharArray().Select(x=>(byte)x).ToArray(); fs.Write(b, 0, b.Length); } } 

我猜你会想要使用FileStream类并寻找你的位置,并放置你的更新数据。