按编号编辑文件中的行

时间:2017-05-29 18:33:21

标签: c#

我必须编写一个字符串的实现,它将它的值存储在硬盘而不是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();
    }
}

我面临的问题是在尝试编辑一行时(在索引器的设置部分)我最后添加一行而不是编辑旧行(这很明显为什么,我只是不知道如何解决它。)

3 个答案:

答案 0 :(得分:1)

您的数组旨在使用整数。这样的类很容易创建,因为所有数字的长度都是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非常适合编写和阅读数字。

开放流是一项非常繁重的操作。因此,当您创建类时,请执行一次。在工作结束时,你需要自己清理。所以我实现了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

答案 1 :(得分:0)

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

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

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

不言而喻,应该进行一些安全检查以检查文件是否存在以及index < allLinesInFile.Length

答案 2 :(得分:0)

我认为,为了排序算法的功课,你不必担心自己的内存大小问题。

当然请添加现有的检查文件以供阅读。

注意:示例中的行计数从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]);                
   }
}
相关问题