Skip to content

Commit fbbc7f2

Browse files
authored
feat: Add WriteLines/AppendLines/Truncate extension methods (#46)
1 parent d169c4d commit fbbc7f2

File tree

4 files changed

+342
-25
lines changed

4 files changed

+342
-25
lines changed
Lines changed: 104 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,130 @@
1-
using System.Collections;
2-
using System.Collections.Generic;
1+
using System.Collections.Generic;
32
using System.Text;
43

54
namespace System.IO.Abstractions
65
{
76
public static class IFileInfoExtensions
87
{
98
/// <summary>
10-
/// Throws an exception if the file <paramref name="info"/> doesn't exists
9+
/// Throws an exception if the <paramref name="file"/> doesn't exists
1110
/// </summary>
12-
/// <param name="info">File that will be checked for existance</param>
11+
/// <param name="file">File that will be checked for existance</param>
1312
/// <exception cref="FileNotFoundException">Exception thrown if the file is not found</exception>
14-
public static void ThrowIfNotFound(this IFileInfo info)
13+
public static void ThrowIfNotFound(this IFileInfo file)
1514
{
16-
if (!info.Exists)
17-
throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", info.FullName));
15+
if (!file.Exists)
16+
throw new FileNotFoundException(StringResources.Format("COULD_NOT_FIND_FILE_EXCEPTION", file.FullName));
1817
}
1918

2019
/// <summary>
21-
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file
20+
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="file"/>
2221
/// </summary>
23-
/// <param name="info">File to enumerate content</param>
22+
/// <param name="file">File to enumerate content</param>
2423
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns>
25-
public static IEnumerable<string> EnumerateLines(this IFileInfo info)
24+
public static IEnumerable<string> EnumerateLines(this IFileInfo file)
2625
{
27-
return new LineEnumerable(info, null);
26+
return new LineEnumerable(file, null);
2827
}
2928

3029
/// <summary>
31-
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the <paramref name="info"/> file
30+
/// Creates an <see cref="IEnumerable{String}"/> that can enumerate the lines of text in the specified <paramref name="file"/>
3231
/// using the specified <paramref name="encoding"/>
3332
/// </summary>
34-
/// <param name="info">File to enumerate content</param>
33+
/// <param name="file">File to enumerate content</param>
3534
/// <returns>Returns an <see cref="IEnumerable{String}"/> to enumerate the content of the file</returns>
36-
public static IEnumerable<string> EnumerateLines(this IFileInfo info, Encoding encoding)
35+
public static IEnumerable<string> EnumerateLines(this IFileInfo file, Encoding encoding)
3736
{
38-
return new LineEnumerable(info, encoding);
37+
return new LineEnumerable(file, encoding);
38+
}
39+
40+
/// <summary>
41+
/// Opens a <see cref="FileSystemStream"/> for the <paramref name="file"/> in the specified <paramref name="mode"/>
42+
/// </summary>
43+
/// <param name="file">File to open stream on</param>
44+
/// <param name="mode">Mode to use when opening the file</param>
45+
/// <returns>A <see cref="FileSystemStream"/> that can read or write data to the specified <paramref name="file"/></returns>
46+
public static FileSystemStream OpenFileStream(this IFileInfo file, FileMode mode)
47+
{
48+
return file.FileSystem.FileStream.New(file.FullName, mode);
49+
}
50+
51+
/// <summary>
52+
/// Creates a new empty <paramref name="file"/>.
53+
/// If the file already exists, the file is truncated.
54+
/// </summary>
55+
/// <param name="file">File to create</param>
56+
/// <returns>The original <see cref="IFileInfo"/> so that methods calls can be chained</returns>
57+
public static IFileInfo Truncate(this IFileInfo file)
58+
{
59+
using(var stream = file.OpenFileStream(FileMode.Create))
60+
{
61+
stream.Dispose();
62+
}
63+
64+
return file;
65+
}
66+
67+
/// <summary>
68+
/// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/> using the UTF-8 encoding.
69+
/// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated.
70+
/// </summary>
71+
/// <param name="file">File to write to</param>
72+
/// <param name="lines">Lines to write to file as text</param>
73+
/// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param>
74+
/// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception>
75+
public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, bool overwrite = false)
76+
{
77+
using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite)))
78+
using (var writer = new StreamWriter(stream))
79+
foreach(var line in lines)
80+
{
81+
writer.WriteLine(line);
82+
}
83+
}
84+
85+
/// <summary>
86+
/// Writes the specified <paramref name="lines"/> to the specified <paramref name="file"/>
87+
/// using the specified <paramref name="encoding"/>.
88+
/// If the file already exists and the <paramref name="overwrite"/> flag is set to true, the file will be truncated.
89+
/// </summary>
90+
/// <param name="file">File to write to</param>
91+
/// <param name="lines">Lines to write to file as text</param>
92+
/// <param name="encoding">Encoding to use when writing the <paramref name="lines"/> to the text file</param>
93+
/// <param name="overwrite">Flag that specifies if the file can be overwritten if it exists</param>
94+
/// <exception cref="IOException">Exception thrown if the file already exists and the <paramref name="overwrite"/> flag is set to <see cref="false"/></exception>
95+
public static void WriteLines(this IFileInfo file, IEnumerable<string> lines, Encoding encoding, bool overwrite = false)
96+
{
97+
using (var stream = file.OpenFileStream(GetWriteFileMode(file, overwrite)))
98+
using (var writer = new StreamWriter(stream, encoding))
99+
foreach (var line in lines)
100+
{
101+
writer.WriteLine(line);
102+
}
103+
}
104+
105+
/// <summary>
106+
/// Appends the specified <paramref name="lines"/> to the specified <paramref name="file"/>
107+
/// </summary>
108+
/// <param name="file">File to append to</param>
109+
/// <param name="lines">Lines to append to file as text</param>
110+
public static void AppendLines(this IFileInfo file, IEnumerable<string> lines)
111+
{
112+
using (var writer = file.AppendText())
113+
foreach (var line in lines)
114+
{
115+
writer.WriteLine(line);
116+
}
117+
}
118+
119+
private static FileMode GetWriteFileMode(IFileInfo info, bool overwrite)
120+
{
121+
if (!overwrite && info.Exists)
122+
{
123+
throw new IOException(StringResources.Format("CANNOT_OVERWRITE", info.FullName));
124+
}
125+
126+
//if the file already exists it will be truncated
127+
return FileMode.Create;
39128
}
40129
}
41130
}

src/System.IO.Abstractions.Extensions/Resources.Designer.cs

Lines changed: 10 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/System.IO.Abstractions.Extensions/Resources.resx

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,13 +117,16 @@
117117
<resheader name="writer">
118118
<value>System.Resources.ResXResourceWriter, System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089</value>
119119
</resheader>
120+
<data name="CANNOT_OVERWRITE" xml:space="preserve">
121+
<value>The file '{0}' already exists.</value>
122+
</data>
120123
<data name="COULD_NOT_FIND_FILE_EXCEPTION" xml:space="preserve">
121124
<value>Could not find file '{0}'.</value>
122125
</data>
123126
<data name="COULD_NOT_FIND_PART_OF_PATH_EXCEPTION" xml:space="preserve">
124127
<value>Could not find a part of the path '{0}'.</value>
125128
</data>
126129
<data name="NOT_AN_ANCESTOR" xml:space="preserve">
127-
<value>'{0}' is not an ancestor of '{1}'</value>
130+
<value>'{0}' is not an ancestor of '{1}'.</value>
128131
</data>
129132
</root>

0 commit comments

Comments
 (0)