Advertisement
sashomaga

Delete words in text files

Jan 27th, 2013
52
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.48 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.IO;
  4. using System.Text.RegularExpressions;
  5. using System.Security;
  6. //Write a program that removes from a text file all words listed in given another text file. Handle all possible exceptions in your methods.
  7. class RemoveWords
  8. {
  9.     static void Main()
  10.     {
  11.         try
  12.         {
  13.             List<string> words = new List<string>();
  14.             using (StreamReader reader = new StreamReader("words.txt"))
  15.             {
  16.                 ReadWords(reader, words);
  17.             }
  18.             using (StreamReader reader = new StreamReader("file.txt"))
  19.             using (StreamWriter writer = new StreamWriter("result.txt"))
  20.             {
  21.                 DeleteMatches(reader, writer, words);
  22.             }
  23.         }
  24.         catch (FileNotFoundException err)
  25.         {
  26.             Console.WriteLine(err.Message);
  27.         }
  28.         catch (DirectoryNotFoundException err)
  29.         {
  30.             Console.WriteLine(err.Message);
  31.         }
  32.         catch (IOException err)
  33.         {
  34.             Console.WriteLine(err.Message);
  35.         }      
  36.         catch (UnauthorizedAccessException err)
  37.         {
  38.             Console.WriteLine(err.Message);
  39.         }
  40.         catch (ArgumentNullException err)
  41.         {
  42.             Console.WriteLine(err.Message);
  43.         }
  44.         catch (ArgumentException err)
  45.         {
  46.             Console.WriteLine(err.Message);
  47.         }
  48.         catch (OutOfMemoryException err)
  49.         {
  50.             Console.WriteLine(err.Message);
  51.         }
  52.         catch (NotSupportedException err)
  53.         {
  54.             Console.WriteLine(err.Message);
  55.         }
  56.         catch (SecurityException err)
  57.         {
  58.             Console.WriteLine(err.Message);
  59.         }
  60.     }
  61.  
  62.     private static void DeleteMatches(StreamReader reader, StreamWriter writer, List<string> words)
  63.     {
  64.         string line;
  65.         while ((line = reader.ReadLine()) != null)
  66.         {
  67.             foreach (string item in words)
  68.             {
  69.                 line = Regex.Replace(line, "\\b" + item + "\\b", string.Empty);
  70.             }
  71.             writer.WriteLine(line);
  72.         }
  73.     }
  74.  
  75.     private static void ReadWords(StreamReader reader, List<string> words)
  76.     {
  77.         string line;
  78.         while ((line = reader.ReadLine()) != null)
  79.         {
  80.             string[] word = line.Split();
  81.             foreach (string item in word)
  82.             {
  83.                 words.Add(item);
  84.             }
  85.         }
  86.     }  
  87. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement