Advertisement
vlad0

Text files 06

Jan 23rd, 2013
192
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.80 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.  
  7. namespace _06.SortNames
  8. {
  9.     class Program
  10.     {
  11.         static void Main(string[] args)
  12.         {
  13.  
  14.             string firstFileName = "../../mytemp01.txt";//the dir of the .cs file
  15.             string newFileName = "../../ConcatenatedFile.txt"; //the dir of the .cs file
  16.  
  17.             Archive(newFileName);
  18.  
  19.             List<string> listOrder = SortFile(firstFileName);
  20.  
  21.             listOrder.Sort();
  22.  
  23.             WriteToFile(listOrder, newFileName);
  24.  
  25.             PrintFile(newFileName);
  26.  
  27.         }
  28.  
  29.         private static void WriteToFile(List<string> listOrder, string newFileName)
  30.         {
  31.             using (StreamWriter destinationFile = new StreamWriter(newFileName))
  32.             {
  33.                 for (int i = 0; i < listOrder.Count; i++)
  34.                 {
  35.                     destinationFile.WriteLine(listOrder[i]);
  36.                 }
  37.             }
  38.         }
  39.  
  40.         private static List<string> SortFile(string firstFileName)
  41.         {
  42.             List<string> listOrder = new List<string>();
  43.  
  44.             using (StreamReader sourceFile = new StreamReader(firstFileName))
  45.             {
  46.                 string line = sourceFile.ReadLine();
  47.                 while (line!= null)
  48.                 {
  49.                     listOrder.Add(line);
  50.                     line = sourceFile.ReadLine();
  51.                 }
  52.             }
  53.  
  54.             return listOrder;
  55.         }
  56.  
  57.         private static void PrintFile(string newFileName)
  58.         {
  59.             Console.Write("Do You want to read the concatenated file <y/n>: ");
  60.             char answer = (char)Console.Read();
  61.  
  62.             if (answer == 'y')
  63.             {
  64.                 using (StreamReader concatenated = new StreamReader(newFileName))
  65.                 {
  66.                     string line = concatenated.ReadLine();
  67.  
  68.                     while (line != null)
  69.                     {
  70.                         Console.WriteLine(line);
  71.                         line = concatenated.ReadLine();
  72.                     }
  73.                 }
  74.             }
  75.             else
  76.             {
  77.                 Console.WriteLine("GoodBye");
  78.             }
  79.         }
  80.  
  81.  
  82.         private static void Archive(string newFileName)
  83.         {
  84.             if (File.Exists(newFileName)) //if the file already exists we make an archive
  85.             {
  86.                 string archiveName = newFileName.Remove(0, 6);
  87.                 string archive = "../../archive" + "-" +DateTime.Now.Hour + "-" + DateTime.Now.Day + "-" + DateTime.Now.Minute + "-" + DateTime.Now.Second + archiveName;
  88.                 File.Copy(newFileName, archive);
  89.                 File.Delete(newFileName);
  90.             }
  91.  
  92.             File.Create(newFileName).Close();
  93.         }
  94.     }
  95. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement