按编号编辑文件中的一行

Editing a line in a file by its number

我必须编写一个字符串的实现,将它的值存储在硬盘而不是 ram 上(我知道这听起来很愚蠢,但它的目的是告诉我们不同的排序算法如何在 ram 和硬盘上工作)。这是我到目前为止写的:

class HDDArray : IEnumerable<int>
{
    private string filePath;

    public int this[int index]
    {
        get
        {
            using (var reader = new StreamReader(filePath))
            {
                string line = reader.ReadLine();

                for (int i = 0; i < index; i++)
                {
                    line = reader.ReadLine();
                }

                return Convert.ToInt32(line);
            }
        }
        set
        {
            using (var fs = File.Open(filePath, FileMode.OpenOrCreate, FileAccess.ReadWrite))
            {
                var reader = new StreamReader(fs);
                var writer = new StreamWriter(fs);

                for (int i = 0; i < index; i++)
                {
                    reader.ReadLine();
                }

                writer.WriteLine(value);
                writer.Dispose();
            }
        }
    }

    public int Length
    {
        get
        {
            int length = 0;

            using (var reader = new StreamReader(filePath))
            {
                while (reader.ReadLine() != null)
                {
                    length++;
                }
            }

            return length;
        }
    }

    public HDDArray(string file)
    {
        filePath = file;

        if (File.Exists(file))
            File.WriteAllText(file, String.Empty);
        else
            File.Create(file).Dispose();
    }

    public IEnumerator<int> GetEnumerator()
    {
        using (var reader = new StreamReader(filePath))
        {
            string line;
            while ((line = reader.ReadLine()) != null)
            {
                yield return Convert.ToInt32(line);
            }
        }
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }
}

我面临的问题是在尝试编辑一行时(在索引器的设置部分)我最终添加了一个新行而不是编辑旧行(很明显为什么,我可以想办法解决它)。

我有待更正,但我认为没有一种简单的方法可以重写特定行,因此您可能会发现重写文件更容易 - 修改该行。

您可以按如下方式更改设置代码:

  set
  {
    var allLinesInFile = File.ReadAllLines(filepath);
    allLinesInFile[index] = value;
    File.WriteAllLines(filepath, allLinesInFile);
  }

不言而喻,那里应该有一些安全检查来检查文件是否存在 index < allLinesInFile.Length

我认为为了排序算法的作业,您不必为内存大小问题而烦恼。

当然请添加检查文件是否存在以供阅读。

注意:示例中的行数从0开始。

string[] lines = File.ReadAllLines(filePath);

using (StreamWriter writer = new StreamWriter(filePath))
{
   for (int currentLineNmb = 0; currentLineNmb < lines.Length; currentLineNmb++ )
   {
       if (currentLineNmb == lineToEditNmb)
       {
          writer.WriteLine(lineToWrite);
          continue;
       }
       writer.WriteLine(lines[currentLineNmb]);                
   }
}

您的数组设计用于处理整数。这样的 class 很容易创建,因为所有数字的长度都是 4 个字节。

class HDDArray : IEnumerable<int>, IDisposable
{
    readonly FileStream stream;
    readonly BinaryWriter writer;
    readonly BinaryReader reader;

    public HDDArray(string file)
    {
        stream = new FileStream(file, FileMode.Create, FileAccess.ReadWrite);
        writer = new BinaryWriter(stream);
        reader = new BinaryReader(stream);
    }

    public int this[int index]
    {
        get
        {
            stream.Position = index * 4;
            return reader.ReadInt32();
        }
        set
        {
            stream.Position = index * 4;
            writer.Write(value);
        }
    }

    public int Length
    {
        get
        {
            return (int)stream.Length / 4;
        }
    }

    public IEnumerator<int> GetEnumerator()
    {
        stream.Position = 0;
        while (reader.PeekChar() != -1)
            yield return reader.ReadInt32();
    }

    IEnumerator IEnumerable.GetEnumerator()
    {
        return GetEnumerator();
    }

    public void Dispose()
    {
        reader?.Dispose();
        writer?.Dispose();
        stream?.Dispose();
    }
}

由于每个数组元素的大小都是已知的,我们可以简单地通过更改其 Position 属性.

来移动到流

BinaryWriterBinaryReader写数字很舒服

开流是一个很繁重的操作。因此在创建 class 时执行一次。在工作结束时,您需要自己进行清理。所以我实现了IDisposable接口。

用法:

HDDArray arr = new HDDArray("test.dat");

Console.WriteLine("Length: " + arr.Length);

for (int i = 0; i < 10; i++)
    arr[i] = i;

Console.WriteLine("Length: " + arr.Length);

foreach (var n in arr)
    Console.WriteLine(n);

// Console.WriteLine(arr[20]); // Exception!

arr.Dispose(); // release resources