Advertisement
sashomaga

TextFiles

Jan 27th, 2013
44
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.82 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text;
  4. //Write a program that reads a text file and inserts line numbers in front of each of its lines.
  5. //The result should be written to another text file.
  6. class InsertLineNumbers
  7. {
  8.     static void Main()
  9.     {      
  10.         using (StreamWriter writer = new StreamWriter("new.txt"))
  11.         using (StreamReader reader = new StreamReader("streamreader.txt"))
  12.         {
  13.             StringBuilder builder = new StringBuilder();
  14.             string line;
  15.             int lineNum = 1;
  16.             while ((line = reader.ReadLine()) != null)
  17.             {
  18.                 builder.Append(lineNum).Append(". ").Append(line);
  19.                 writer.WriteLine(builder.ToString());
  20.                 builder.Clear();
  21.                 lineNum++;
  22.             }
  23.         }
  24.     }
  25. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement