简体   繁体   English

异步读写文本行

[英]async reading and writing lines of text

I've found plenty of examples of how to read/write text to a file asynchronously, but I'm having a hard time finding how to do it with a List. 我已经找到了许多有关如何异步地将文本写入文件的示例,但是我很难找到如何使用列表来处理文本的方法。

For the writing I've got this, which seems to work: 对于写作,我已经知道了,这似乎可行:

public async Task<List<string>> GetTextFromFile(string file)
{
    using (var reader = File.OpenText(file))
    {
        var fileText = await reader.ReadToEndAsync();
        return fileText.Split(new[] { Environment.NewLine }, StringSplitOptions.None).ToList();
    }
}

The writing is a bit trickier though ... 虽然写作有点棘手...

public async Task WriteTextToFile(string file, List<string> lines, bool append)
{
    if (!append && File.Exists(file)) File.Delete(file);
    using (var writer = File.OpenWrite(file))
    {
        StringBuilder builder = new StringBuilder();
        foreach (string value in lines)
        {
            builder.Append(value);
            builder.Append(Environment.NewLine);
        }
        Byte[] info = new UTF8Encoding(true).GetBytes(builder.ToString());
        await writer.WriteAsync(info, 0, info.Length);
    }
}

My problem with this is that for a moment it appears my data is triple in memory. 我的问题是,有一段时间我的数据似乎在内存中是三倍的。 The original List of my lines, then the StringBuilder makes it a single string with the newlines, then in info I have the byte representation of the string. 我的行的原始列表,然后由StringBuilder将其与换行符组合成单个字符串,然后在信息中,我具有该字符串的字节表示形式。 That seems excessive that I have to have three copies of essentially the same data in memory. 我必须在内存中拥有三个基本相同的数据副本,这似乎太过分了。

I am concerned with this because at times I'll be reading and writing large text files. 我对此很担心,因为有时我会读写大型文本文件。

Following up on that, let me be clear - I know that for extremely large text files I can do this all line by line. 接下来,请让我清楚-我知道对于超大型文本文件,我可以逐行完成。 What I am looking for are two methods of reading/writing data. 我正在寻找的是两种读取/写入数据的方法。 The first is to read in the whole thing and process it, and the second is to do it line by line. 第一个是阅读全文并进行处理,第二个是逐行进行。 Right now I am working on the first approach for my small and moderate sized text files. 现在,我正在为中小型文本文件开发第一种方法。 But I am still concerned with the data replication issue. 但是我仍然担心数据复制问题。

The following might suit your needs as it does not store the data again as well as writing it line by line: 以下内容可能会满足您的需要,因为它不会再次存储数据,也不会逐行写入数据:

public async Task WriteTextToFile(string file, List<string> lines, bool append)
{
    if (!append && File.Exists(file))
        File.Delete(file);

    using (var writer = File.OpenWrite(file))
    {
        using (var streamWriter = new StreamWriter(writer))
            foreach (var line in lines)
                await streamWriter.WriteLineAsync(line);
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM