A FileReader : StreamReader : File Directory Stream C# Examples


C# Examples » File Directory Stream » StreamReader »

 

A FileReader





To perform character-based file operations, you wrap a FileStream inside either a StreamReader or a StreamWriter.




    
using  System;  
using  System.IO;    
  
class  MainClass  {  
    public  static  void  Main()  {  
        FileStream  fin;  
        string  s;  
  
        try  {  
            fin  =  new  FileStream("test.txt",  FileMode.Open);  
        }  
        catch(FileNotFoundException  exc)  {  
            Console.WriteLine(exc.Message  +  "Cannot  open  file.");  
            return  ;  
        }    
  
        StreamReader  fstr_in  =  new  StreamReader(fin);  
  
        //  Read  the  file  line-by-line.  
        while((s  =  fstr_in.ReadLine())  !=  null)  {  
            Console.WriteLine(s);  
        }  
  
        fstr_in.Close();  
    }  
}
    
   
  
   



Output

asdfasdf


HTML code for linking to this page:

Follow Navioo On Twitter

C# Examples

 Navioo File Directory Stream
» StreamReader