Advertisement
vlad0

Text Files 02

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