Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add append file methods #31

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
60 changes: 60 additions & 0 deletions src/PCLStorage.Abstractions/FileExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -46,5 +46,65 @@ public static async Task WriteAllTextAsync(this IFile file, string contents)
}
}
}

/// <summary>
/// Opens a file, appends the specified string to the file, and then closes the file.
/// </summary>
/// <param name="file">The file to write to</param>
/// <param name="contents">The content to write to the file</param>
/// <returns>A task which completes when the write operation finishes</returns>
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);
}
}
}

/// <summary>
/// Appends lines to a file, and then closes the file.
/// </summary>
/// <param name="file">The file to write to</param>
/// <param name="contents">The content to write to the file</param>
/// <returns>A task which completes when the write operation finishes</returns>
public static async Task AppendAllLinesAsync(this IFile file, IEnumerable<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);
}
}
}
}

/// <summary>
/// Appends lines to a file, and then closes the file.
/// </summary>
/// <param name="file">The file to write to</param>
/// <param name="contents">The content to write to the file</param>
/// <returns>A task which completes when the write operation finishes</returns>
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);
}
}
}
}
}
}
57 changes: 57 additions & 0 deletions test/PCLStorage.Test/FileTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,63 @@ public async Task GetFileThrowsWhenFileDoesNotExist()
await ExceptionAssert.ThrowsAsync<FileNotFoundException>(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<string>(){"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()
{
Expand Down