Hi Guest

MobileUser

1 HomePage 1 Jobs 1 WalkIn 1Articles  

Cat Exam

 :) Latest Topic
AJAX
VB.NET
SQL Query
UDDI
CLASS
JavaScript Questions
SharePoint Interview
SilverLight
WCF
general knowledge
 ASP.NET
 :) Hot Jobs
 
 :) Latest Articles


  

 


StreamReader and StreamWriter with example
Question Posted on 26 Aug 2011

Home >> DotNet >> C# >> StreamReader and StreamWriter with example




StreamWriter and StreamReader taking twice the memory footprint in some cases over the basic FileStream. StreamReader is designed for character input in a particular encoding, whereas the Stream class is designed for byte input and output.StreamReader is nothing but file pointer.
StreamReader is used to read data from the file.
StreamWriter is used to write data into the file.

StreamReader methods:-Below are the some main methods in streamreader
1)Read() : Reads single character.
2)ReadLine() : Reads single line.
3)ReadToEnd() : Reads full file.

StreamWriter methods:-Below are the some main methods in streamWriter
1)Write()
2)WriteLine()

Example:-Below is example to streamreader text file
using System;
using System.IO;
class Teststreamreader
{
 public static void Main()
 {
  try
  {
   // create instance of streamreader.
   using (StreamReader stread = new StreamReader("FileName.txt"))
   {
    string strline;
    // Read and display lines of file
    while ((strline = stread.ReadLine()) != null)
    {
     Console.WriteLine(strline);
    }
   }
  }
  catch (Exception e)
  {
   Console.WriteLine("Problem with file could not be read:");
   Console.WriteLine(e.Message);
  }
 }
}

Example:-Below is example to streamwriter on text file
using System;
using System.IO;
public class testStearmwriter
{
 public static void Main(String[] args)
 {
  FileStream sb = new FileStream("FileName.txt", FileMode.OpenOrCreate);
  char[] b = {'b','b','c','d','e','f','g','h','i','j','k','l','m'};
  StreamWriter sw = new StreamWriter(sb);
  sw.Write(b, 3, 8);
  sw.Close();
 }
}
0
0



 
 Input Your Comments Or suggestions(If Any)
 Name
 Email(optional)
 Comments


Other Important Questions
Simplest definition of partial classes and its rules?
Definition of Tuple class with some examples?
Define Race Condition in cSharp?
C#.Net Reserve Words
can we inherit private class level variables in c#






 
Top Searches:asp net questions vb net questions sql query uddl questions class javascript Questions sharepoint interview questions and concept silverlight questions and concept wcf questions beans general knowledge ajax questions
PHP | Biztalk | Testing | SAP | HR |