Text File Splitter - C#.NET Custom Events

I recently received a a large text file (many megabytes) which was a concatenation of many different smaller files.  The smaller files where of different size and the separated by a particular character. I did a quick Google search, but almost all of the file splitting applications available split files based on size.

I needed to split a file based on a certain ASCII character.

image

Written using C#, my text file splitting app is extremely simple.

Step 1:

Read the entire text file into a string variable

  1.     TextReader tr = new StreamReader(filePath);
  2.     string file = tr.ReadToEnd();

Step 2:

Using the string function 'Split', create a string array.

  1.     string[] files = file.Split(splitChar);

Step 3:

Iterate through the string array, and write each to a separate file.

  1.     foreach (string file in files)
  2.     {
  3.         if (file.Length != 0)
  4.         {
  5.             if (CreateFile(file, string.Format("{0}\\output_{1}.{2}", outputDir, _writtenCount.ToString(), fileExt)))
  6.             {
  7.                 _writtenCount++;
  8.             }
  9.             else
  10.             {
  11.                 _errorCount++;
  12.             }
  13.         }
  14.         else
  15.         {
  16.             _errorCount++;
  17.         }
  18.     }

That's it!!!

While there is some beauty in simplistic design, the differentiating aspect of this application is the status screen.

image

All of the file reading\splitting\writing is contained within the class 'Splitter'.  I have tried to keep it independent of the application so that
it could be re-used within other applications if needed.  Thus it cannot know about the application, it's forms or controls.

However, I do want my applications (that use this class) to provide the user some feedback on how the splitting process is proceeding.  I have achieved this by raising 'events' within the splitter class.

 

First of all the event delegate's need to be defined, they are defined globally but still within the 'splitter' class.

  1.     public delegate void FileProcessedHandler(object sender);
  2.     public delegate void FileWrittenHandler(object sender);
  3.     public delegate void FileCompleteHandler(object sender);

Then the events need to be declared within the class.

  1.     public event FileProcessedHandler FileProcessedEvent;
  2.     public event FileWrittenHandler FileWrittenEvent;
  3.     public event FileCompleteHandler FileCompleteEvent;

Then the events can be raised within the code of the class.

  1. public void ProcessFile(string filename, char splitCharacter, string outputDirectory, string outputExtension)
  2. {
  3.     string file = ReadFile(filename);
  4.     string[] splitFiles = SplitFile(file, splitCharacter.ToString().ToCharArray());
  5.     _fileCount = splitFiles.Length;
  6.     FileProcessedEvent(this);
  7.     SaveFiles(splitFiles, outputDirectory, outputExtension);
  8.     FileCompleteEvent(this);
  9. }

If you would like to download the application, or the full source code, please go to the Text File Splitter Project.

Trackback URL for this post:

http://ontheperiphery.veraida.com/trackback/35
from How To Get Out Of Debt on Mon, 01/03/2010 - 06:35
How To Get out of debt - The complete guide to getting out of debt & staying out of debt. Say goodbye to credit card debt forever...