Using Read method
using System.IO;
using System;// Program to read every line in the file using Read method of StreamReader
class ReadFile
{
static void Main()
{
string fileloc="D://MyFile.txt"; // File location
// creates file
FileStream fs = new FileStream(fileloc, FileMode.Create);
fs.Close();
Console.WriteLine("File created successfully");
// writes to file
StreamWriter sw = new StreamWriter(fileloc);
sw.WriteLine("first line");
sw.WriteLine("second line");
sw.Flush();
sw.Close();
Console.WriteLine("Written to file successfully");
// Lets read the file now
Console.WriteLine("Content of file :");
StreamReader sr = new StreamReader(fileloc);
while (sr.Peek() >= 0)
{
Console.Write((char)sr.Read()); // Reads as character by character
}
Console.ReadLine();
}
}
Output:
File created successfully
Written to file successfully
Content of file:
first line
second line