Write a Text File with UTF8 Encoding,
- public void WriteATxtFileWithUTF8Encoding()
- {
- string txtFilePath = @"D:\bnarayan.txt";
- using (StreamWriter writer =
- new StreamWriter(txtFilePath, true, Encoding.UTF8))
- {
- writer.WriteLine("Name: Banketeshvar Narayan");
- writer.WriteLine("Email Id: [email protected]");
- }
- }
Read Data from Text File With UTF8 Encoding,
- public void ReadDataFromTxtFileWithUTF8Encoding(string txtFilePath)
- {
- if (File.Exists(txtFilePath))
- {
- using(var reader = new StreamReader(File.OpenRead(txtFilePath), Encoding.UTF8))
- {
- while (!reader.EndOfStream)
- {
- var line = reader.ReadLine();
- if (!String.IsNullOrEmpty(line))
- {
- Console.WriteLine(line);
- }
- }
- }
- }
- }
Calling Read and Write methods for Text Files
- static void Main(string[] args)
- {
- Program p = new Program();
- p.WriteATxtFileWithUTF8Encoding();
- p.ReadDataFromTxtFileWithUTF8Encoding(@"D:\bnarayan.txt");
- }
714