From 8935cab9685344a1e07458b9b6d1419d2c22674f Mon Sep 17 00:00:00 2001 From: Squall Leonhart Date: Thu, 28 Jan 2016 11:45:44 +0700 Subject: [PATCH] Add append file methods --- src/PCLStorage.Abstractions/FileExtensions.cs | 60 +++++++++++++++++++ test/PCLStorage.Test/FileTests.cs | 57 ++++++++++++++++++ 2 files changed, 117 insertions(+) diff --git a/src/PCLStorage.Abstractions/FileExtensions.cs b/src/PCLStorage.Abstractions/FileExtensions.cs index 033fd8c..9df03cd 100644 --- a/src/PCLStorage.Abstractions/FileExtensions.cs +++ b/src/PCLStorage.Abstractions/FileExtensions.cs @@ -46,5 +46,65 @@ public static async Task WriteAllTextAsync(this IFile file, string contents) } } } + + /// + /// Opens a file, appends the specified string to the file, and then closes the file. + /// + /// The file to write to + /// The content to write to the file + /// A task which completes when the write operation finishes + public static async Task AppendAllTextAsync(this IFile file, string contents) + { + using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) + { + stream.Seek(stream.Length, SeekOrigin.Begin); + using (var sw = new StreamWriter(stream)) + { + await sw.WriteAsync(contents).ConfigureAwait(false); + } + } + } + + /// + /// Appends lines to a file, and then closes the file. + /// + /// The file to write to + /// The content to write to the file + /// A task which completes when the write operation finishes + public static async Task AppendAllLinesAsync(this IFile file, IEnumerable contents) + { + using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) + { + stream.Seek(stream.Length, SeekOrigin.Begin); + using (var sw = new StreamWriter(stream)) + { + foreach (var content in contents) + { + await sw.WriteLineAsync(content).ConfigureAwait(false); + } + } + } + } + + /// + /// Appends lines to a file, and then closes the file. + /// + /// The file to write to + /// The content to write to the file + /// A task which completes when the write operation finishes + public static async Task AppendAllLinesAsync(this IFile file, params string[] contents) + { + using (var stream = await file.OpenAsync(FileAccess.ReadAndWrite).ConfigureAwait(false)) + { + stream.Seek(stream.Length, SeekOrigin.Begin); + using (var sw = new StreamWriter(stream)) + { + foreach (var content in contents) + { + await sw.WriteLineAsync(content).ConfigureAwait(false); + } + } + } + } } } diff --git a/test/PCLStorage.Test/FileTests.cs b/test/PCLStorage.Test/FileTests.cs index b462eba..ff88c7a 100644 --- a/test/PCLStorage.Test/FileTests.cs +++ b/test/PCLStorage.Test/FileTests.cs @@ -35,6 +35,63 @@ public async Task GetFileThrowsWhenFileDoesNotExist() await ExceptionAssert.ThrowsAsync(async () => await folder.GetFileAsync(fileName)); } + [TestMethod] + public async Task AppendFile() + { + // Arrange + IFolder folder = TestFileSystem.LocalStorage; + string fileName = "fileToAppend.txt"; + + // Act + IFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.FailIfExists); + await file.AppendAllTextAsync("Test"); + var text = await file.ReadAllTextAsync(); + + // Assert + Assert.AreEqual("Test", text); + + // Act + await file.AppendAllTextAsync("Test"); + text = await file.ReadAllTextAsync(); + + // Assert + Assert.AreEqual("TestTest", text); + + + // Cleanup + await file.DeleteAsync(); + } + + [TestMethod] + public async Task AppendLines() + { + // Arrange + IFolder folder = TestFileSystem.LocalStorage; + string fileName = "fileToAppend.txt"; + string expected = "Test" + Environment.NewLine + "Test" + Environment.NewLine; + + // Act + IFile file = await folder.CreateFileAsync(fileName, CreationCollisionOption.ReplaceExisting); + await file.AppendAllLinesAsync("Test", "Test"); + var text = await file.ReadAllTextAsync(); + + // Assert + Assert.AreEqual(expected, text); + + // Act + var lines = new List(){"Test", "Test"}; + await file.AppendAllLinesAsync(lines); + expected += expected; + text = await file.ReadAllTextAsync(); + + // Assert + Assert.AreEqual(expected, text); + + + // Cleanup + await file.DeleteAsync(); + } + [TestMethod] public async Task CreateFile() {