简体   繁体   中英

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. 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);
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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