How to use the StreamReader.Peek method in C#

1 Answer

0 votes
using System;
using System.IO;

namespace ConsoleApplication1
{
    class Program
    {
        static void Main(string[] args)
        {
            string path = @"d:\test.txt";

            try
            {
                using (StreamWriter sw = new StreamWriter(path))
                {
                    sw.WriteLine("C#");
                    sw.WriteLine("Programming");
                    sw.WriteLine("Is Good For Business");
                }

                using (StreamReader sr = new StreamReader(path))
                {
                    while (sr.Peek() > -1)
                           Console.WriteLine(sr.ReadLine());
                }
            }
            catch (Exception e)
            {
                Console.WriteLine(e.ToString());
            }
        }
    }
}
/*
run:
 
C#
Programming
Is Good For Business

*/



answered Sep 21, 2014 by avibootz

Related questions

...