Language
日本語
English

Caution

JavaScript is disabled in your browser.
This site uses JavaScript for features such as search.
For the best experience, please enable JavaScript before browsing this site.

C# Dictionary

  1. Home
  2. C# Dictionary
  3. StreamReader / StreamWriter

StreamReader / StreamWriter

How to use StreamReader for reading files line by line, and StreamWriter for writing files line by line. Both classes let you process large files without loading the entire contents into memory.

Syntax

using System.IO;

// Open a file for reading with StreamReader (automatically closed by using).
using StreamReader reader = new StreamReader(string path);
using StreamReader reader = new StreamReader(string path, Encoding encoding);

// Read one line (returns null at end of file).
string? line = reader.ReadLine();

// Read the rest of the file as a single string.
string allContent = reader.ReadToEnd();

// Open a file for writing with StreamWriter.
using StreamWriter writer = new StreamWriter(string path);

// Open in append mode (false to overwrite).
using StreamWriter writer = new StreamWriter(string path, bool append);

// Write one line (with a newline).
writer.WriteLine(string value);

// Write without a trailing newline.
writer.Write(string value);

Method List

Method / PropertyDescription
new StreamReader(path)Opens the specified file for reading. The default encoding is UTF-8.
ReadLine()Reads and returns one line. Returns null when the end of the file is reached.
ReadToEnd()Returns all content from the current position to the end of the file as a string.
EndOfStreamA property that returns true when the end of the stream has been reached.
new StreamWriter(path)Opens the specified file for writing (overwrites any existing file).
new StreamWriter(path, append)Opens in append mode if append is true, or overwrite mode if false.
WriteLine(value)Writes value followed by a newline.
Write(value)Writes value without a trailing newline.

Sample Code

using System;
using System.IO;
using System.Text;

string filePath = "log.txt";

// Create and write to the file using StreamWriter.
using (StreamWriter writer = new StreamWriter(filePath, false, Encoding.UTF8))
{
    writer.WriteLine("Line 1: Start logging");
    writer.WriteLine("Line 2: Processing");
    writer.WriteLine("Line 3: Processing complete");
}
Console.WriteLine("Write complete");

// Read the file line by line using StreamReader.
Console.WriteLine("--- Read all lines ---");
using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
{
    string? line;
    int lineNumber = 1;
    while ((line = reader.ReadLine()) != null)
    {
        Console.WriteLine($"{lineNumber}: {line}");
        lineNumber++;
    }
}

// Append a new line in append mode.
using (StreamWriter writer = new StreamWriter(filePath, append: true, Encoding.UTF8))
{
    writer.WriteLine("Line 4: Appended");
}

// Read everything at once using ReadToEnd().
using (StreamReader reader = new StreamReader(filePath, Encoding.UTF8))
{
    Console.WriteLine("--- ReadToEnd() ---");
    Console.WriteLine(reader.ReadToEnd());
}

// Clean up by deleting the file.
File.Delete(filePath);

Notes

Always wrap StreamReader and StreamWriter in a using block. If you omit using and leave the file open, other processes will be unable to access it. Also, because StreamWriter uses an internal buffer, Flush() is called automatically when the using block exits (at Dispose() time), ensuring all buffered data is written to the file.

For simple cases where you want to read or write the entire file at once, File.ReadAllText() / WriteAllText() is more concise. For checking whether a file exists or deleting files, see File.Exists() / Delete() / Copy().

If you find any errors or copyright issues, please .