Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
Node is saved as draft in My Content >> Draft
  • How to Reading from and Writing to text file in c#

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 196
    Comment on it

    To read and write data in text file we need to use .NET classes meant for file reading and writing.

    Following are the classes which are used for reading from and writing data to text files we uses. These classes inherits from class stream, which supports reading and writing bytes into a file stream.

    • StreamReader : This class reads a series of characters from the file
    • StreamWriter : This class writes the data into the text file

     

     

    The StreamReader Class Methods:

     

    Sr.No. Methods
    1 public override void Close()

    This will closes the StreamReader object and the underlying stream, and releases any system resources associated with the reader.

    2 public override int Peek()

    It Returns the next available character but does not consume it.

    3 public override int Read()

    It will advances the character position by one after Reading the next character from the input stream.

     

     

     class Program
       {
          static void Main(string[] args)
          {
             try
             {
                // Create an instance of StreamReader to read from a file.
                // The using statement also closes the StreamReader.
                using (StreamReader sr = new StreamReader("c:/mydemo.txt"))
                {
                   string line;
                   
                   // Read and display lines from the file until 
                   // the end of the file is reached. 
                   while ((line = sr.ReadLine()) != null)
                   {
                      Console.WriteLine(line);
                   }
                }
             }
             catch (Exception e)
             {
                
                // Let the user know what went wrong.
                Console.WriteLine("The file could not be read:");
                Console.WriteLine(e.Message);
             }
             
             Console.ReadKey();
          }
       }

     

     

    The StreamWriter Class

     

    Sr.No. Methods
    1 public override void Close()

    This methods will closes the current StreamWriter object also the underlying stream.

    2 public override void Flush()

    This method will clears all buffers for the current writer and causes any buffered data to be written to the underlying stream.

    3 public virtual void Write(bool value)

    This Method Writes the text representation of a Boolean value to the text string or stream. (Inherited from TextWriter.)

    4 public override void Write(char value)

    This Method Writes a character to the stream.

    5 public virtual void Write(decimal value)

    This Method Writes the text representation of a decimal value to the text string or stream.

    6 public virtual void Write(double value)

    This Method Writes the text representation of an 8-byte floating-point value to the text string or stream.

    7 public virtual void Write(int value)

    This Method Writes the text representation of a 4-byte signed integer to the text string or stream.

    8 public override void Write(string value)

    This Method Writes a string to the stream.

    9 public virtual void WriteLine()

    This Method Writes a line terminator to the text string or stream.

     

    class Program
       {
          static void Main(string[] args)
          {
          
             string[] names = new string[] {"Zara Ali", "Nuha Ali"};
             using (StreamWriter sw = new StreamWriter("names.txt"))
             {
                
                foreach (string s in names)
                {
                   sw.WriteLine(s);
                }
             }
             
             // Read and show each line from the file.
             string line = "";
             using (StreamReader sr = new StreamReader("names.txt"))
             {
                while ((line = sr.ReadLine()) != null)
                {
                   Console.WriteLine(line);
                }
             }
             
             Console.ReadKey();
          }
       }

     

    .net

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: