Sunday, 11 October 2015

Read a File contents and Write a contents to a File using C#

In this post we are going to see how to create a C#  programming to read a file and write a File. Normally the contents of the File is written as Stream to the File. Every file have one kind of format to represent that file. Now we create a utility class which will create a file and write the contents to that file and also read the contents of the File.


File utility:
*******************
using System;
using System.IO;
using System.Text;

namespace ReadWrite
{
    public class FileUtility:IDisposable
    {
        private string _filepath;

        public string FilePath {
            set {
                if (_filepath != value)
                {
                    _filepath = value;                   
                }
            }
            get { return _filepath; }
        }

        TextWriter writer = null;
        TextReader reader = null;

        public FileUtility(string path)
        {
            FilePath = path;                
        }

        public string ReadLine() {
            string line = string.Empty;
            using (reader = new StreamReader(this.FilePath))
            {
                line = reader.ReadLine();
            }
            return line;
        }

        public string ReadFullText() {

            StringBuilder text = new StringBuilder();
            using (reader = new StreamReader(this.FilePath))
            {                
                var line = reader.ReadLine();
                while (line != null)
                {
                    text.AppendLine(line);
                    line = reader.ReadLine();
                }
            }
            return text.ToString();

        }


        public void Write(string content) {
            using (writer = new StreamWriter(this.FilePath, true))
            {
               writer.Write(content);
            }
        }

        public void WriteLine(string content) {
            using (writer = new StreamWriter(this.FilePath, true))
            {
                writer.WriteLine(content);
            }
        }

        public void Dispose()
        {
            if (reader != null)
            {
                reader.Dispose();
                reader = null;
            }

            if (writer != null)
            {
                writer.Dispose();
                writer = null;
            }

        }
    }
}


Program.cs
*****************
class Program
    {
        static void Main(string[] args)
        {
            FileUtility file = new FileUtility(@"D:\sample.txt");
            file.WriteLine("Hi");
            file.WriteLine("This is a");
            file.Write("sample");
            file.Dispose();

            string data = file.ReadFullText();
            Console.WriteLine(data);

            Console.Read();
        }
    }



Output;
**************



From this post we can learn how to create a program which can read and write a file.

No comments:

Post a Comment