First run:
The second runtime:
Copy codeThe Code is as follows: using System;
Using System. Collections. Generic;
Using System. Linq;
Using System. Text;
Using System. IO;
Namespace File Operations
{
Class Program
{
Static void Main (string [] args)
{
// Create a text file. You 'd better judge it first.
StreamWriter sw;
If (! File. Exists ("templog.txt "))
{
// Create a text file if it does not exist and write some content
Sw = File. CreateText ("templog.txt ");
Sw. Write ("First word ");
Sw. WriteLine ("follow the boss .");
Sw. WriteLine ("current date is :");
Sw. WriteLine (DateTime. Now );
}
Else
{
// Add some text if any
Sw = File. AppendText ("templog.txt ");
For (int I = 0; I <10; I ++)
{
Sw. WriteLine ("{0} can be output as usual to the screen", I );
}
}
Sw. Close ();
// Create a reader
StreamReader sr = new StreamReader ("templog.txt ");
// Read all at once
Console. WriteLine (sr. ReadToEnd ());
Console. ReadLine ();
}
}
}