Complete the code to open a file stream using a using statement.
using (var fileStream = new FileStream("example.txt", [1])) { // Read or write operations }
The FileStream constructor requires a FileMode to specify how to open the file. FileMode.Open opens an existing file.
Complete the code to write text to a file using a StreamWriter inside a using statement.
using (var writer = new StreamWriter(new FileStream("output.txt", FileMode.Create))) { writer.[1]("Hello, world!"); }
WriteLine writes a line of text to the file. It is the correct method to write text inside the using block.
Fix the error in the using statement to properly dispose the FileStream after reading.
FileStream fs = new FileStream("data.txt", FileMode.Open); using ([1]) { // Read from fs }
The using statement should use the existing fs variable to ensure it is disposed properly.
Fill both blanks to create a using statement that reads all text from a file.
using (var reader = new StreamReader(new FileStream("file.txt", [1]))) { string content = reader.[2](); }
FileMode.Open opens the file for reading, and ReadToEnd() reads all text from the file.
Fill all three blanks to write multiple lines to a file using a using statement and a loop.
using (var writer = new StreamWriter(new FileStream("log.txt", [1]))) { foreach (var line in lines) { writer.[2](line); } writer.[3](); }
FileMode.Append opens the file to add text at the end. WriteLine writes each line, and Flush ensures all data is written to the file.