How to write and read a simple text file with StreamWriter and StreamReader in C#

1 Answer

0 votes
using System;
using System.IO;

namespace Text_File
{
	class Class1
	{
		static void Main(string[] args)
		{
			StreamWriter sw = new StreamWriter("Test.txt");
			sw.WriteLine("The First Line");
			sw.WriteLine("The Second Line");
			sw.WriteLine("The Third Line");
			sw.Write('A');
			sw.Write('B');
            sw.Write('C');
            sw.Write('D');
			sw.Write(sw.NewLine);
			sw.WriteLine("Last line");
			sw.Close();
			StreamReader sr = new StreamReader("Test.txt");
			string s;
			do
			{
				s = sr.ReadLine();
				Console.WriteLine(s);
			}
			while(s != null);
			Console.WriteLine("File length = {0} Bytes",sr.BaseStream.Length);
			sr.Close();				
		}
	}
}
/*
The First Line
The Second Line
The Third Line
ABCD
Last line

File length = 66 Bytes

*/


answered Aug 15, 2014 by avibootz

Related questions

1 answer 266 views
1 answer 191 views
1 answer 277 views
1 answer 197 views
1 answer 287 views
1 answer 315 views
315 views asked Aug 7, 2014 by avibootz
1 answer 136 views
...