Reading and Writing Data – C# – part 3 (Text Files)

Text Files

StreamReader, StreamWriter, main methods:

  • ReadLine(), WriteLine()
FileInfo sourceFile = new FileInfo(@"C:\test\someFile.cs");

StreamReader reader = sourceFile.OpenText();

/* The second parameter is the Boolean argument append. If the file already exists, true will cause
the new data to be appended to the end of the file, and false will casue the file to be overwritten. */
StreamWriter writer = new StreamWriter(@"C:\test\someFile.bak", false);

string text;

do
{
   text = reader.ReadLine();
   writer.WriteLine(text);
} while (text != null);

reader.Close();
writer.Close();
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s