Reading and Writing Files

Reading and Writing Files:
Reading and writing files in C# is very easy. For reading and writing files in C# you have to include a namespace in your application that is System.IO. This namespace has classes that are used for reading and writing files.                                                                                                          

StreamReader          For reading data from files
StreamWriter           For writing data into the files
Reading data form Files:
Now lets look at an example which reads a file and calculates number of lines,words,characters and number of asteriks and displays the result after closing the file.Data is read character by character.Read() method returns int value so this is converted to char data type.
    class Program
    {
       
        public void OpenFile()
        {
            StreamReader sr;
         
            int words = 0, lines = 0, charc = 0, stars = 0;
            char read;
            string Filepath = @"F:\Antivirus\My Documents\ASSEMBLY ALGOS\4proc.asm";

            sr = new StreamReader(Filepath);
            while (sr.Peek() != -1)
            {
                read = (char)sr.Read();
                charc++;
                if (read == '*')
                    stars++;
                else if (read == ' ')
                    words++;
                else if (read == '\n')
                    lines++;
            }
            sr.Close();
           
            Console.WriteLine("Number of lines are {0} ",lines.ToString());
            Console.WriteLine("Number of words are {0} ",words.ToString());
            Console.WriteLine("Number of characters are {0} ",charc.ToString());
            Console.WriteLine("Number of stars are {0} ", stars.ToString());
         
        }
        static void Main(string[] args)
        {
            Program obj = new Program();
            obj.OpenFile();
        }
       }
Writing Data To Files:
   class Program
    {
         
        public void WriteFile()
        {
            StreamWriter sw;
         
           
            string Filepath = @"F:\abc.txt";

           sw = new StreamWriter(Filepath);
           sw.WriteLine("*********Starting to write*********");  
           sw.WriteLine("My name is Mairaj Ahmed Minhas");
           sw.WriteLine("This is first line ");
           sw.WriteLine("Writing data to file");
           sw.WriteLine("Three lines are written ");
           sw.WriteLine("************Finished******************");
           sw.Close();
            Console.WriteLine("File written");
          
        }
static void Main(string[] args)
        {
            Program obj = new Program();
           
            obj.WriteFile();
        }
}
You must close the file after you have finished reading or writing the file. While writing a file if you do not close that file no data will be written.

Comments

Popular posts from this blog

Using Progress Bar In C#

Get elements by class name in javascript

Jquery serer side datatables in asp.net