Wednesday, March 7, 2012

READING MULTIPLE TEXT FILES AND SAVING THE CONTENT TO A SINGLE FILE


First step is selecting the file paths and then read the file using Stream Reader and save the content to the string builder.  After doing this for all the files, now our content is in the string builder.
Now create a file with the file stream (using file stream is easily can easily kill the process).  Now write the content of the string builder to the newly created file using the Stream Writer.
I have done this in winforms.  You can download the demo here.
Our UI looks like this.



            UI consists of two buttons, one(btnBrowse) for browsing or selecting the text files and another(btnSaveResult) for saving the new file with the combined content.  One textbox for viewing the combined content.  Apart from these controls we have added one OpenFileDialog control and one SaveFileDialog control for selecting the files and saving the newly created file respectively.
Now lets jump into the code.
Using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace ReadingTextFiles
{
    public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void btnBrowse_Click(object sender, EventArgs e)
        {
            ofdTextFile.ShowDialog();
        }

        private void ofdTextFile_FileOk(object sender, CancelEventArgs e)
        {
            List<string> myfiles = new List<string>();
            StringBuilder sb = new StringBuilder(“hi”);
            foreach (string path in ofdTextFile.FileNames)
            {
                myfiles.Add(path);
                string filepath = path;
                StreamReader myfile = new StreamReader(filepath);
                sb.Append(myfile.ReadToEnd());
                myfile.Close();
            }
           
           
            //txtEditor.Text = filepath;
           
            txtEditor.Text = sb.ToString();
        }

        private void btnSaveResult_Click(object sender, EventArgs e)
        {
            saveFileDialog.ShowDialog();
        }

        private void saveFileDialog_FileOk(object sender, CancelEventArgs e)
        {
            FileStream fs;
            using (fs = File.Create(saveFileDialog.FileName.ToString()))
            {
            }

            using (StreamWriter sw = new StreamWriter(saveFileDialog.FileName.ToString()))
            {
                sw.Write(txtEditor.Text);
            }
        }
    }
}


  • Opening the file dialog on the btnBrowse Click.
  • On file selection completed we are getting the selected file paths, reading the content of each file by appending it to the string builder and finally displaying the string builder content to the text box (txtEditor).
  • Opening the save dialog on the btnSaveResult Click.
  • On file saving we are creating a file with name specified in the save file dialog and after creating the file, writing the content to the newly created file.
That’s it..
Happy Coding….