Professional Documents
Culture Documents
aspx
Introduction
Text files provide a common denominator format where both people and programs can read and
understand. The .NET Framework includes convenience classes that make reading and writing text files
very easy. The following sequence outlines the basic steps necessary to work with text files:
It's that simple. Listing 1 shows how to write text data to a file.
using System;
using System.IO;
namespace csharp_station.howto
{
class TextFileWriter
{
static void Main(string[] args)
{
// create a writer and open the file
TextWriter tw = new StreamWriter("date.txt");
This program creates a text file when it runs. In the directory where the executable program is located,
you'll find a file named date.txt. If you view the contents of this file, you'll see the following textual
representation of the date and time when the program last ran:
2/15/2002 8:54:51 PM
The first task in Listing 1 is to open the file. This happens by instantiating a StreamWriter class, which returns an
1 de 3 1/9/2011 19:01
C# Station: Reading and Writing Text Files http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx
object of type TextWriter. The result could have also been assigned to a StreamWriter instance. The StreamWriter
was called with a single parameter, indicating the name of the file to open. If this file doesn't exist, the StreamWriter
will create it. The StreamWriter also has 6 other constructor overloads that permit you to specify the file in different
ways, buffer info, and text encoding. Here's the line that opens the date.txt file:
Using the TextWriter instance, tw, you can write text info to the file. The example writes the text for the current
date and time, using the static Now property of the DateTime class. Here's the line from the code:
tw.WriteLine(DateTime.Now);
tw.Close();
using System;
using System.IO;
namespace csharp_station.howto
{
class TextFileReader
{
static void Main(string[] args)
{
// create reader & open file
Textreader tr = new StreamReader("date.txt");
In Listing 2, the text file is opened in a manner similar to the method used in Listing 1, except it uses a
StreamReader class constructor to create an instance of a Textreader. The StreamReader class includes
additional overloads that allow you to specify the file in different ways, text format encoding, and buffer
info. This program opens the date.txt file, which should be in the same directory as the executable file:
Within a Console.WriteLine statement, the program reads a line of text from the file, using the
ReadLine() method of the Textreader instance. The Textreader class also includes methods that allow you
to invoke the Read() method to read one or more character or use the Peek() method to see what the
next character is without pulling it from the stream. Here's the code that reads an entire line from the
text file:
Console.WriteLine(tr.ReadLine());
2 de 3 1/9/2011 19:01
C# Station: Reading and Writing Text Files http://www.csharp-station.com/HowTo/ReadWriteTextFile.aspx
tr.Close();
Summary
This article showed how to write text to a file and read it back out. For more details on additional
methods, consult the .NET Frameworks reference on the StreamWriter, StreamReader, TextWriter, and
Textreader classes.
3 de 3 1/9/2011 19:01