-
Notifications
You must be signed in to change notification settings - Fork 6
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Add WriteLines/AppendLines/Truncate extension methods (#46)
- Loading branch information
Showing
4 changed files
with
342 additions
and
25 deletions.
There are no files selected for viewing
119 changes: 104 additions & 15 deletions
119
src/System.IO.Abstractions.Extensions/IFileInfoExtensions.cs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,41 +1,130 @@ | ||
using System.Collections; | ||
using System.Collections.Generic; | ||
using System.Collections.Generic; | ||
using System.Text; | ||
|
||
namespace System.IO.Abstractions | ||
{ | ||
public static class IFileInfoExtensions | ||
{ | ||
/// <summary> | ||
/// Throws an exception if the file <paramref name="info"/> doesn't exists | ||
/// Throws an exception if the <paramref name="file"/> doesn't exists | ||
/// </summary> | ||
/// <param name="info">File that will be checked for existance</param> | ||
/// <param name="file">File that will be checked for existance</param> | ||
/// <exception cref="FileNotFoundException">Exception thrown if the file is not found</exception> | ||
public static void ThrowIfNotFound(this IFileInfo info) | ||
public static void ThrowIfNotFound(this IFileInfo file) | ||
{ | ||
if (!info.Exists) | ||
throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", info.FullName)); | ||
if (!file.Exists) | ||
throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", file.FullName)); | ||
} | ||
|
||
/// <summary> | ||
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file | ||
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="file"/> | ||
/// </summary> | ||
/// <param name="info">File to enumerate content</param> | ||
/// <param name="file">File to enumerate content</param> | ||
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns> | ||
public static IEnumerable<string> EnumerateLines(this IFileInfo info) | ||
public static IEnumerable<string> EnumerateLines(this IFileInfo file) | ||
{ | ||
return new LineEnumerable(info, null); | ||
return new LineEnumerable(file, null); | ||
} | ||
|
||
/// <summary> | ||
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file | ||
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the specified <paramref name="file"/> | ||
/// using the specified <paramref name="encoding"/> | ||
/// </summary> | ||
/// <param name="info">File to enumerate content</param> | ||
/// <param name="file">File to enumerate content</param> | ||
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns> | ||
public static IEnumerable<string> EnumerateLines(this IFileInfo info, Encoding encoding) | ||
public static IEnumerable<string> EnumerateLines(this IFileInfo file, Encoding encoding) | ||
{ | ||
return new LineEnumerable(info, encoding); | ||
return new LineEnumerable(file, encoding); | ||
} | ||
|
||
/// <summary> | ||
/// Opens a <see cref="FileSystemStream"/> for the <paramref name="file"/> in the specified <paramref name="mode"/> | ||
/// </summary> | ||
/// <param name="file">File to open stream on</param> | ||
/// <param name="mode">Mode to use when opening the file</param> | ||
/// <returns>A <see cref="FileSystemStream"/> that can read or write data to the specified <paramref name="file"/></returns> | ||
public static FileSystemStream OpenFileStream(this IFileInfo file, FileMode mode) | ||
{ | ||
return file.FileSystem.FileStream.New(file.FullName, mode); | ||
} | ||
|
||
/// <summary> | ||
/// Creates a new empty <paramref name="file"/>. | ||
/// If the file already exists, the file is truncated. | ||
/// </summary> | ||
/// <param name="file">File to create</param> | ||
/// <returns>The original <see cref="IFileInfo"/> so that methods calls can be chained</returns> | ||
public static IFileInfo Truncate(this IFileInfo file) | ||
{ | ||
using(var stream = file.OpenFileStream(FileMode.Create)) | ||
{ | ||
stream.Dispose(); | ||
} | ||
|
||
return file; | ||
} | ||
|
||
/// <summary> | ||
/// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/> using the UTF-8 encoding. | ||
/// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated. | ||
/// </summary> | ||
/// <param name="file">File to write to</param> | ||
/// <param name="lines">Lines to write to file as text</param> | ||
/// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param> | ||
/// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception> | ||
public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, bool overwrite = false) | ||
{ | ||
using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite))) | ||
using (var writer = new StreamWriter(stream)) | ||
foreach(var line in lines) | ||
{ | ||
writer.WriteLine(line); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/> | ||
/// using the specified <paramref name="encoding"/>. | ||
/// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated. | ||
/// </summary> | ||
/// <param name="file">File to write to</param> | ||
/// <param name="lines">Lines to write to file as text</param> | ||
/// <param name="encoding">Encoding to use when writing the <paramref name="lines"/> to the text file</param> | ||
/// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param> | ||
/// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception> | ||
public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, Encoding encoding, bool overwrite = false) | ||
{ | ||
using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite))) | ||
using (var writer = new StreamWriter(stream, encoding)) | ||
foreach (var line in lines) | ||
{ | ||
writer.WriteLine(line); | ||
} | ||
} | ||
|
||
/// <summary> | ||
/// Appends the specified <paramref name="lines"/> to the specified <paramref name="file"/> | ||
/// </summary> | ||
/// <param name="file">File to append to</param> | ||
/// <param name="lines">Lines to append to file as text</param> | ||
public static void AppendLines(this IFileInfo file, IEnumerable<string> lines) | ||
{ | ||
using (var writer = file.AppendText()) | ||
foreach (var line in lines) | ||
{ | ||
writer.WriteLine(line); | ||
} | ||
} | ||
|
||
private static FileMode GetWriteFileMode(IFileInfo info, bool overwrite) | ||
{ | ||
if (!overwrite && info.Exists) | ||
{ | ||
throw new IOException(StringResources.Format("CANNOT_OVERWRITE", info.FullName)); | ||
} | ||
|
||
//if the file already exists it will be truncated | ||
return FileMode.Create; | ||
} | ||
} | ||
} |
11 changes: 10 additions & 1 deletion
11
src/System.IO.Abstractions.Extensions/Resources.Designer.cs
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.