ellapt

T13.6.ReadSortWrite

Jan 31st, 2013
57
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.69 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4.  
  5. class ReadSortWrite
  6. {
  7. static void SortFileByStrings(string inpFile, string resFile)
  8. {
  9. List <string> lines = new List<string>();
  10. string line = "";
  11. try
  12. {
  13. System.Text.Encoding encodingCyr = System.Text.Encoding.GetEncoding(1251);
  14.  
  15. StreamReader streamReader = new StreamReader(inpFile, encodingCyr);
  16.  
  17. using (streamReader)
  18. {
  19. line = streamReader.ReadLine();
  20. lines.Add(line);
  21. while (line != null)
  22. {
  23. Console.WriteLine(line);
  24. line = streamReader.ReadLine();
  25. lines.Add(line);
  26. }
  27. }
  28.  
  29. lines.Sort();
  30.  
  31. Console.Write("\nSorted file: ");
  32. StreamWriter streamWriter = new StreamWriter(resFile, false, encodingCyr);
  33. using (streamWriter)
  34. {
  35. foreach (string lin in lines)
  36. {
  37. streamWriter.Write(lin+"\n");
  38. Console.WriteLine(lin);
  39. }
  40. }
  41. }
  42. catch (System.Exception exc)
  43. {
  44. Console.WriteLine("File input/output operation failed: {0}, ", exc.Message);
  45. }
  46. }
  47.  
  48. static void Main()
  49. {
  50. Console.WriteLine("Read text file with list of strings, sort and save them to another text file");
  51. Console.WriteLine("Input file:");
  52. string inpFile = @"../../words.txt";
  53. string sortedFile = @"../../sortedWords.txt";
  54. SortFileByStrings(inpFile, sortedFile);
  55. }
  56. }
Advertisement
Add Comment
Please, Sign In to add comment