November 24, 2019

How to Read/Write text file in line by line manner using C#

  November 24, 2019
What if our data is not stored in a format oriented file types like Excel,Json or XML ?
There should be a way to read content from a normal text file line by line.

Below methods help us read/write data to .txt file in a line by line manner

Write to text file

Below method helps us to write each input to next line by passing the file path and the string to write as parameters.
It also creates the file if it already does't exist in the target location.

        public void WriteToTextFile(String writeData, string fileName)
        {

            var dir = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");

            Directory.CreateDirectory(dir + "\\TempData\\");

            string strFilePath = dir + "\\TempData\\" + fileName + ".txt";

            try
            {
                if (!Directory.Exists(dir))
                {
                    Directory.CreateDirectory(dir);
                }

                TextWriter tw = new StreamWriter(strFilePath, true);

                // write a line of text to the file
                tw.WriteLine(writeData);

                // close the stream
                tw.Close();

            }
            catch (Exception ex)
            {

                WriteLog("fail", ex.Message);
            }
        }

Read from text file line by line

Now lets checkout how can we read the contents of the text file in a line by line manner and extract it of List of string. For this we need to pass the path of the file to read as a parameter.

       public IList<String> ReadFromTextFile(string fileName)
        {

            var dir = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");

            Directory.CreateDirectory(dir + "\\TempData\\");

            string strFilePath = dir + "\\TempData\\" + fileName + ".txt";

            return File.ReadAllLines(strFilePath, Encoding.UTF8);

        }

Delete a file if already exists

Sometimes there comes a scenario where we needs to delete the already existing instead of appending data to it.
We can use below code for those kind of situations.


        public void DeleteTextFile(string fileName)
        {

            var dir = AppDomain.CurrentDomain.BaseDirectory.Replace("\\bin\\Debug", "");

            Directory.CreateDirectory(dir + "\\TempData\\");

            string strFilePath = dir + "\\TempData\\" + fileName + ".txt";

            if (File.Exists(strFilePath))
            {
                // If file found, delete it    
                File.Delete(strFilePath);
            }

        }
logoblog

Thanks for reading How to Read/Write text file in line by line manner using C#

Previous
« Prev Post

No comments:

Post a Comment

Bookmark this website for more workarounds and issue fixes.

Verify Zip file contents without extracting using C# + Selenium

While doing automation testing, we may get a scenario where we need to download a zip file and to validate the contents inside it. One way t...