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();

Recommended Answers

All 3 Replies

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

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());
        }
    }
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.