Read text file line by line with exception catch

image_pdfimage_print
   
 

using System;
using System.IO;

class Class1{
      static void Main(string[] args){
         string strLine;
         try
         {
            FileStream aFile = new FileStream("books.xml",FileMode.Open);
            StreamReader sr = new StreamReader(aFile);
            strLine = sr.ReadLine();

            while(strLine != null)
            {
               Console.WriteLine(strLine);
               strLine = sr.ReadLine();
            }
            aFile.Close();
            sr.Close();
         }
         catch(IOException e)
         {
            Console.WriteLine("An IO exception has been thrown!");
            Console.WriteLine(e.ToString());
            return;
         }
         return;
     }
}


           
         
     


This entry was posted in File Stream. Bookmark the permalink.