C-Sharp | Java | Python | Swift | GO | WPF | Ruby | Scala | F# | JavaScript | SQL | PHP | Angular | HTML
Using: This keyword means different things in different places. Near StreamWriter, it controls low-level resource usage.
UsingFile: A new StreamWriter is initialized with the file name "important.txt". Three writes are done using StreamWriter.
Write, WriteLine: The Write method does not append a newline. The WriteLine methods append a newline "\r\n" at each call.
C# program that uses StreamWriter
using System.IO;
class Program
{
static void Main()
{
using (StreamWriter writer = new StreamWriter("important.txt"))
{
writer.Write("Word ");
writer.WriteLine("word 2");
writer.WriteLine("Line");
}
}
}
Output
(Text is in "important.txt" file.)
Word word 2
Line
New: A new StreamWriter is initialized and it opens "C:\\log.txt" for appending. The second argument (true) specifies an append operation.
Append: The first line is appended to the file. If it is empty, the file begins with that string.
Next: The second using construct reopens the "C:\\log.txt" file and then appends another string to it.
Note: If for some reason the file was deleted, the line will be added just the same.
C# program that appends to file
using System.IO;
class Program
{
static void Main()
{
// Write single line to new file.
using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("Important data line 1");
}
// Append line to the file.
using (StreamWriter writer = new StreamWriter("C:\\log.txt", true))
{
writer.WriteLine("Line 2");
}
}
}
Output
(File "log.txt" contains these lines.)
Important data line 1
Line 2
Var: We use var, which makes the syntax shorter but equivalent in functionality. The file "loop.txt" is opened only once for writing.
VarFormat: The format string can be easily used with Write. You have to specify a substitution marker {0} in the first parameter.
string.FormatC# program that loops with StreamWriter
using System.IO;
class Program
{
static void Main()
{
// Use var type which is shorter.
using (var writer = new StreamWriter("loop.txt"))
{
// Loop through ten numbers.
for (int i = 0; i < 10; i++)
{
// Write format string to file.
writer.Write("{0:0.0} ", i);
}
}
}
}
Output
(These numbers are in the file "loop.txt".)
0.0 1.0 2.0 3.0 4.0 5.0 6.0 7.0 8.0 9.0
Note: String interpolation is about as fast as the format syntax shown above. It is a good alternative.
String InterpolationC# program that uses StreamWriter, string interpolation
using System.IO;
class Program
{
static void Main()
{
using (StreamWriter writer = new StreamWriter("C:\\programs\\file.txt"))
{
string animal = "cat";
int size = 12;
// Use string interpolation syntax to make code clearer.
writer.WriteLine($"The {animal} is {size} pounds.");
}
}
}
Output
The cat is 12 pounds.
Tip: If your program does lots of writes, it will manage system resources correctly if you use using.
Tip: Finally statements always run—they are stubborn about that. They will not run only in an Environment.Exit call or a crash.
Finally