I have been trying to understand these methods so hard but have failed so far. Please, if you can, give me a example with the explanation. Thanks for your time.

Following methods of StreamWriter class:
1)Read();
2)Peek();

The methods you mention are or can come from the StreamReader class.
Look them up to see what they do on MSDN here

Edited 4 Years Ago by ddanbe: n/a

For
Peek-Returns the next available character but does not consume it.-immediately you call this once again returns same value because position not moved
Read-Reads the next character from the input stream and advances the character position by one character. -imediately you call this once again returns different (next) value because position moved

hi ..

Peek and read are methods to read data from a file,which has to be achieved by importing IO namespace where it has class files like
*Stream reader
*stream writer class files
Stream reader class enables the methods like peek, read to read data from a file.

here is an example ..hope this would make you clear

using System;
using System.IO;

class Test 
{
	
    public static void Main() 
    {
        string path = @"c:\temp\MyTest.txt";

        try 
        {
            if (File.Exists(path)) 
            {
                File.Delete(path);
            }

            using (StreamWriter sw = new StreamWriter(path)) 
            {
                sw.WriteLine("This");
                sw.WriteLine("is some text");
                sw.WriteLine("to test");
                sw.WriteLine("Reading");
            }

            using (StreamReader sr = new StreamReader(path)) 
            {

                while (sr.Peek() > -1) 
                {
                    Console.WriteLine(sr.ReadLine());
                }
            }
        } 
        catch (Exception e) 
        {
            Console.WriteLine("The process failed: {0}", e.ToString());
        }
    }
}

Edited 4 Years Ago by kamilacbe: n/a

This article has been dead for over six months. Start a new discussion instead.