Advertisement
hristo_bratanov

Concatenate two text files in one

Jan 23rd, 2013
557
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.23 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4.  
  5. class ConcatenateTextFiles
  6. {
  7.     static void Main()
  8.     {
  9.         StreamReader readerFirstFile = new StreamReader("subs.txt", Encoding.GetEncoding("windows-1251"));
  10.         StreamReader readerSecondFile = new StreamReader("subs(2).txt", Encoding.GetEncoding("windows-1251"));
  11.         using (readerFirstFile)    // read and write first file
  12.         {
  13.             StreamWriter writer = new StreamWriter("conc.txt", false, Encoding.GetEncoding("windows-1251"));
  14.             using (writer)
  15.             {
  16.                 string s;
  17.                 while ((s = readerFirstFile.ReadLine()) != null)
  18.                 {
  19.                     writer.WriteLine(s);
  20.                 }
  21.  
  22.             }
  23.            
  24.         }
  25.         using (readerSecondFile)   //read and append(true) second file to first file
  26.         {
  27.             StreamWriter writer = new StreamWriter("conc.txt", true, Encoding.GetEncoding("windows-1251"));
  28.             using (writer)
  29.             {
  30.                 string s;
  31.                 while ((s = readerSecondFile.ReadLine()) != null)
  32.                 {
  33.                     writer.WriteLine(s);
  34.                 }
  35.  
  36.             }
  37.            
  38.         }
  39.  
  40.     }
  41. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement