1

我有一个 txt 文件,其中包含如下数据:

:10FF800000040B4E00040B4E00047D1400047D148D
:10FF900000040B4E0004CF6200040B4E00040B4E15
:10FFA00000040B4E00040B4E00040B4E00040B4EDD
:10FFB00000047D1400047D1400047D1400047D14ED
:10FFC00000040B4E000000000000000000000000D4
:10FFD0000000000000040B4E0000000000000000C4
:10FFE0000000000000000000000000000000000011
:10FFF0000000000000000000060000000000BFF844
:020000020000FC
:020000040014E6
:043FF0005AC8A58C7A
:00000001FF

我想用我的 C# 程序做的是在特定行之后或之前添加一行,假设添加行:

:020000098723060

在此行之前:

:020000020000FC

我试过使用 File.ReadLines("file.txt").Last(); 但这只是给了我最后一个,如果我想要第三个或第四个怎么办?另外,有什么方法可以识别文件中的“:”吗?

4

6 回答 6

11

The simplest way - if you're happy to read the whole file into memory - would be just:

public void InsertLineBefore(string file, string lineToFind, string lineToInsert)
{
    List<string> lines = File.ReadLines(file).ToList();
    int index = lines.IndexOf(lineToFind);
    // TODO: Validation (if index is -1, we couldn't find it)
    lines.Insert(index, lineToInsert);
    File.WriteAllLines(file, lines);
}

public void InsertLineAfter(string file, string lineToFind, string lineToInsert)
{
    List<string> lines = File.ReadLines(file).ToList();
    int index = lines.IndexOf(lineToFind);
    // TODO: Validation (if index is -1, we couldn't find it)
    lines.Insert(index + 1, lineToInsert);
    File.WriteAllLines(file, lines);
}

There are significantly more efficient ways of doing this, but this approach is really simple.

于 2013-01-31T23:08:58.550 回答
3

A brute force approach

string[] lines = File.ReadAllLines("file.txt");
using(StreamWrite sw = new StreamWriter("file.txt"))
{
   foreach(string line in lines)
   {
       if(line == ":020000020000FC")
          sw.WriteLine(":020000098723060");
       sw.WriteLine(line);
   }
}
于 2013-01-31T23:09:18.113 回答
2

我会说最好逐行读取和写入,特别是如果目标文件往往很大:

using (StreamReader r = new StreamReader("Test.txt"))
{
    using (StreamWriter w = new StreamWriter("TestOut.txt"))
    {
        while (!r.EndOfStream)
        {
            string line = r.ReadLine();
            w.WriteLine(line);
            if (line == ":020000020000FC")
                w.WriteLine(":020000098723060");
        }
        w.Close();
        r.Close();
    }
}
于 2013-01-31T23:14:30.973 回答
0

“:”字符并没有真正帮助实现,这些行都已经用换行符分隔了。

这是一种不将其全部加载到内存或输出到不同文件的方法的尝试。

永远不要越过溪流。

static Int32 GetCharPos(StreamReader s)
{
    var ia = BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.GetField;
    Int32 charpos = (Int32)s.GetType().InvokeMember("charPos", ia, null, s, null);
    Int32 charlen = (Int32)s.GetType().InvokeMember("charLen", ia, null, s, null);
    return (Int32)s.BaseStream.Position - charlen + charpos;
}

static void Appsert(string data, string precedingEntry = null)
{
    if (precedingEntry == null)
    {
        using (var filestream = new FileStream(dataPath, FileMode.Append))
        using (var tw = new StreamWriter(filestream))
        {
            tw.WriteLine(data);
            return;
        }
    }

    int seekPos = -1;
    using (var readstream = new FileStream(dataPath, 
        FileMode.Open, FileAccess.Read, FileShare.Write))
    using (var writestream = new FileStream(dataPath, 
        FileMode.Open, FileAccess.Write, FileShare.Read))
    using (var tr = new StreamReader(readstream))
    {
        while (seekPos == -1)
        {
            var line = tr.ReadLine();
            if (line == precedingEntry)
                seekPos = GetCharPos(tr);
            else if (tr.EndOfStream)
                seekPos = (int)readstream.Length;
        }

        writestream.Seek(seekPos, SeekOrigin.Begin);
        readstream.Seek(seekPos, SeekOrigin.Begin);
        int readLength = 0;
        var readBuffer = new byte[4096];
        var writeBuffer = new byte[4096];
        var writeData = tr.CurrentEncoding.GetBytes(data + Environment.NewLine);
        int writeLength = writeData.Length;
        writeData.CopyTo(writeBuffer, 0);
        while (true & writeLength > 0)
        {
            readLength = readstream.Read(readBuffer, 0, readBuffer.Length);
            writestream.Write(writeBuffer, 0, writeLength);
            var tmp = writeBuffer;
            writeBuffer = readBuffer;
            writeLength = readLength;
            readBuffer = tmp;
        }                
    }
}
于 2013-02-01T01:41:42.877 回答
0

这是一个解决方案,虽然它可能不是最好的,但它确实有效:

public void AddTextToFile(string filePath, int lineNumber, string txt) //zero based lineNumber
{
        Collection<string> newLines = new Collection<string>(File.ReadAllLines(filePath).ToList());


        if (lineNumber < newLines.Count)
            newLines.Insert(lineNumber, txt);
        else 
            newLines.Add(txt);

        using (StreamWriter writer = new StreamWriter(filePath, false))
        {
            foreach (string s in newLines)
                writer.WriteLine(s);
        }
}

并且要回答有关确定字符串中是否存在“:”的问题,答案是肯定的,在上面的示例中,您可以通过...检查该行是否包含它

if(newLines[idx].Contains(':'))
//do something
于 2013-02-01T00:21:37.280 回答
0

不确定您是否由于大小等原因试图避免读取整个文件...但是您不能只读取文件然后替换...例如

var text = readFile(somePath);
writeFile( text.replace(":020000020000FC\n",":020000098723060\n:020000020000FC\n") , somePath);
于 2013-01-31T23:17:49.040 回答