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)
{
...
}
}
}
});
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.
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(...);
}
}
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With