Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Read/Write a large text file in WinRT only using StreamReader/StreamWriter?

I try to figure out how I could read and write a large text file in WinRT line by line.

FileIO.ReadLinesAsync respectively FileIO.WriteLinesAsync would not work because they work with a list of strings that is passed respectively returned. With large text files that may cause an OutOfMemoryException.

I could of course write line by line using FileIO.AppendTextAsync but that would be inefficient.

What I figured out was that I could use a StreamReader or StreamWriter like in the sample code below.

But is there really no native Windows Runtime way to achieve this?

I know by the way that Windows Store Apps are not supposed to read or write large text files. I need a solution just because I am writing a recipe book for programmers.

Sample: Reading using a StreamReader:

StorageFile file = ...

await Task.Run(async () =>
{
   using (IRandomAccessStream winRtStream = await file.OpenAsync(FileAccessMode.Read))
   {
      Stream dotnetStream = winRtStream.AsStreamForRead();
      using (StreamReader streamReader = new StreamReader(dotnetStream))
      {
         string line;
         while ((line = streamReader.ReadLine()) != null)
         {
            ...
         }
      }
   }
});
like image 848
Jürgen Bayer Avatar asked Dec 07 '25 02:12

Jürgen Bayer


2 Answers

Actually, your assumpltion about list of string is not completelly correct. FileIO.WriteLinesAsync accepts IEnumerable<string> as a parameter. So you can just do somethig like this:

IEnumerable<string> GenerateLines()
{
    for (int ix = 0; ix < 10000000; ix++)
        yield return "This is a line #" + ix;
}

//....
WriteLinesAsync(file, GenerateLines());

As for reading of a big file, you are right, we need some custom work like you did in your sample.

like image 106
ie. Avatar answered Dec 09 '25 01:12

ie.


Here is my solution for writing large files using a StreamWriter. The used memory stays low even when writing very large files:

StorageFile file = ...

using (var randomAccessStream = await file.OpenAsync(FileAccessMode.ReadWrite))
{
    using (var outputStream = randomAccessStream.GetOutputStreamAt(0))
    {
        using (StreamWriter streamWriter = 
           new StreamWriter(outputStream.AsStreamForWrite()))
        {
            for (...)
            {
                await streamWriter.WriteLineAsync(...);
             }
         }
      }
 }
like image 42
Jürgen Bayer Avatar answered Dec 08 '25 23:12

Jürgen Bayer



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!