illustrates reading and writing text data
/*
Mastering Visual C# .NET
by Jason Price, Mike Gunderloy
Publisher: Sybex;
ISBN: 0782129110
*/
/*
Example15_16.cs illustrates reading and writing text data
*/
using System;
using System.IO;
public class Example15_16
{
public static void Main()
{
// create a new file to work with
FileStream outStream = File.Create("c:\\TextTest.txt");
// use a StreamWriter to write data to the file
StreamWriter sw = new StreamWriter(outStream);
// write some text to the file
sw.WriteLine("This is a test of the StreamWriter class");
// flush and close
sw.Flush();
sw.Close();
// now open the file for reading
StreamReader sr = new StreamReader("c:\\TextTest.txt");
// read the first line of the file into a buffer and display it
string FirstLine;
FirstLine = sr.ReadLine();
Console.WriteLine(FirstLine);
// clean up
sr.Close();
}
}
Related examples in the same category