.NET Tutorials, Forums, Interview Questions And Answers
Welcome :Guest
 
Sign In
Register
 
Win Surprise Gifts!!!
Congratulations!!!


Top 5 Contributors of the Month
david stephan

Home >> Articles >> C# >> Post New Resource Bookmark and Share   

 Subscribe to Articles

Stream I/O Operation (Part-I)

Posted By:Creative13       Posted Date: September 18, 2012    Points: 200    Category: C#    URL: http://www.dotnetspark.com  

The Article demonstrate a way to Copy / Append the one Text Document to another, using StreamReader & StreamWriter Classes.
 

Description:

About Classes used -

#1: StreamReader class provides an access to read the data from Stream such as Text File.
#2: StreamWriter class provides an access to write the data from Stream such as Text File.


Namespace Required         -           System.IO,
System.Diagnostics

Controls Used           -          
            1. TextBox Control (txtInput, txtOutput)
          2. Button Control (btnSelect, btnCreatePDF)
           
         
Here I implemented the Code for copying and appending Text Document to another Text Document.

           
The Code:
1. Variable Declarations
        StreamReader rdr;   //StreamReader Object for Reading File
        StreamWriter wrt;   //StreamWriter Object for Writing File
Listing 1

1. Select the Text Input & Output File (code both for “Select” Button).
           
            //Select File with OpenFileDialog Box
            String SelectFile(TextBox t)
            {
                  using (OpenFileDialog file = new OpenFileDialog())
                  {
                        file.Filter = "Only Text Documents|*.txt";

                        //Show the Dialog Box & Select the File
                        file.ShowDialog();

                        //Return the selected File Name as String
                        return file.FileName;
                  }
      }
Listing 2
   

3. Copy Data from one to another Text File (code for “Copy All” Button).

            //Input File
            rdr = new StreamReader(txtInput.Text);

            //Output File
            wrt = new StreamWriter(txtOutput.Text, false);

            //Copy All Data From Input file to Output file
            wrt.Write(rdr.ReadToEnd().ToString());

            //Close the StreamWriter instance
            wrt.Close();

            MessageBox.Show("File Copied Successfully....");

            //Open the Output file
            Process.Start(txtOutput.Text);
Listing 2


4. Append Data from one to another Text File (code for “Append All” Button).
            //Input File
            rdr = new StreamReader(txtInput.Text);

            //Output File
            wrt = new StreamWriter(txtOutput.Text, true);

            //Append All Data From Input file to Output file
            wrt.WriteLine(Environment.NewLine +
                "--------------- Appended File Start from here ---------------" + 
      Environment.NewLine);
            wrt.Write(rdr.ReadToEnd().ToString());

            //Close the StreamWriter instance
            wrt.Close();

            MessageBox.Show("File Appended Successfully....");

            //Open the Output file
            Process.Start(txtOutput.Text);
Listing 3

3. Now execute the Application and see the result (Figure 1).
         
Intended Result:

Figure 1

Summary:

          In this piece of writing, using C# environment, we have seen basic stream input/output operation for copying & appending text files using StreamWriter & StreamReader classes.

 Subscribe to Articles

     

Further Readings:

Responses

No response found. Be the first to respond this post

Post Comment

You must Sign In To post reply
Find More Articles on C#, ASP.Net, Vb.Net, SQL Server and more Here

Hall of Fame    Twitter   Terms of Service    Privacy Policy    Contact Us    Archives   Tell A Friend