Advertisement
Guest User

CustomFileProcessor

a guest
Feb 23rd, 2019
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.06 KB | None | 0 0
  1. using System;
  2. using System.Collections.Generic;
  3. using System.Linq;
  4. using System.Text;
  5. using System.Threading.Tasks;
  6. using System.IO;
  7. namespace PrzykladOdczytuZPliku
  8. {
  9.     public class CustomFileReader
  10.     {
  11.         private List<string> list1;
  12.  
  13.         private List<string> list2;
  14.  
  15.         public CustomFileReader()
  16.         {
  17.             list1 = new List<string>();
  18.             list2 = new List<string>();
  19.         }
  20.         public void ReadDataFromFile(string path)
  21.         {
  22.             using (var reader = new StreamReader(path))
  23.             {
  24.                 try
  25.                 {
  26.                     while (reader.Peek() != 0)
  27.                     {
  28.                         ParseLine(reader.ReadLine());
  29.                     }
  30.                 }
  31.                 catch (Exception ex)
  32.                 {
  33.                     Console.WriteLine(ex.Message);
  34.                 }
  35.             }
  36.         }
  37.  
  38.         private void ParseLine(string readLine)
  39.         {
  40.             string[] tmp_line = readLine.Split('-');
  41.             list1.Add(tmp_line[0]);
  42.             list2.Add(tmp_line[1]);
  43.         }
  44.  
  45.         public void SaveDataToFile(string path)
  46.         {
  47.             using (var writer = new StreamWriter(path))
  48.             {
  49.                 try
  50.                 {
  51.                     for (int i = 0; i < list1.Count; i++)
  52.                     {
  53.                         var line = new StringBuilder();
  54.                         line.Append(list1[i]);
  55.                         line.Append('-');
  56.                         line.Append(list2[i]);
  57.                         writer.WriteLine(line);
  58.                     }
  59.                 }
  60.                 catch (Exception ex)
  61.                 {
  62.                     Console.WriteLine(ex.Message);
  63.                 }
  64.             }
  65.         }
  66.     }
  67.  
  68.     public class Program
  69.     {
  70.         static void Main(string[] args)
  71.         {
  72.             var fileProcesor = new CustomFileReader();
  73.             fileProcesor.ReadDataFromFile("plik.txt");
  74.             fileProcesor.SaveDataToFile("plik2.txt");
  75.         }
  76.     }
  77. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement