Advertisement
vlad0

Text files 11

Jan 24th, 2013
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.IO;
  6. using System.Text.RegularExpressions;
  7.  
  8. namespace _11.DeleteTestPrefixwords
  9. {
  10.     class DeleteTestPrefixwords
  11.     {
  12.         static void Main(string[] args)
  13.         {
  14.             string firstFileName = "../../mytemp01.txt";//the dir of the .cs file
  15.             EditFile(firstFileName);
  16.  
  17.             PrintFile(firstFileName);
  18.         }
  19.  
  20.         private static void EditFile(string firstFileName)
  21.         {
  22.             List<string> fileInfo = new List<string>();
  23.             using (StreamReader sourceFile = new StreamReader(firstFileName))
  24.             {
  25.                 string line = sourceFile.ReadLine();
  26.                 string pattern = "\\btest\\w*";
  27.                 while (line != null)
  28.                 {
  29.                     Regex rgx = new Regex(pattern);
  30.                     line = rgx.Replace(line, "");
  31.                     fileInfo.Add(line);
  32.                     line = sourceFile.ReadLine();
  33.                 }
  34.             }
  35.  
  36.             using (StreamWriter destinationFile = new StreamWriter(firstFileName))
  37.             {
  38.                 for (int i = 0; i < fileInfo.Count; i++)
  39.                 {
  40.                     destinationFile.WriteLine(fileInfo[i]);
  41.                 }
  42.             }
  43.         }
  44.  
  45.         private static void PrintFile(string newFileName)
  46.         {
  47.             Console.Write("Do You want to read the concatenated file <y/n>: ");
  48.             char answer = (char)Console.Read();
  49.  
  50.             if (answer == 'y')
  51.             {
  52.                 using (StreamReader concatenated = new StreamReader(newFileName))
  53.                 {
  54.                     string line = concatenated.ReadLine();
  55.  
  56.                     while (line != null)
  57.                     {
  58.                         Console.WriteLine(line);
  59.                         line = concatenated.ReadLine();
  60.                     }
  61.                 }
  62.             }
  63.             else
  64.             {
  65.                 Console.WriteLine("GoodBye");
  66.             }
  67.         }
  68.     }
  69. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement