4DM3M

22

Jan 3rd, 2022
973
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.35 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Text.Json;
  4.  
  5. namespace Drafty
  6. {
  7.     class Program
  8.     {
  9.  
  10.         class FileReader
  11.         {
  12.            
  13.             public string FileName { get; set; }
  14.  
  15.             public FileReader()
  16.             {
  17.                 string backup = Deserialize();
  18.                
  19.                 Console.WriteLine($"Введите имя файла, на диске D (прошлое имя файла: {backup}):");
  20.                 Console.WriteLine("Нажмите Enter для выбора предыдущего имени файла.");
  21.  
  22.                 string source = Console.ReadLine();
  23.  
  24.                 if (source.Length == 0)
  25.                 {
  26.                     this.FileName = backup;
  27.                     return;
  28.                 }
  29.  
  30.                 this.FileName = source;
  31.             }
  32.  
  33.             public FileReader(string name)
  34.             {
  35.                 this.FileName = name;
  36.             }
  37.            
  38.             public string[] ReadFile()
  39.             {
  40.                 string path = @$"D:\{FileName}.txt";
  41.  
  42.                 if (!File.Exists(path))
  43.                 {
  44.                     Console.WriteLine("Файл не найден");
  45.                     return null;
  46.                 }
  47.  
  48.                 Serialize();
  49.                 string content = File.ReadAllText(path);
  50.                 return content.Split(' ', StringSplitOptions.RemoveEmptyEntries);
  51.             }
  52.  
  53.             private string Deserialize()
  54.             {
  55.                 string path = @"D:\serialization.json";
  56.  
  57.                 if (File.Exists(path))
  58.                 {
  59.                     return File.ReadAllText(path);
  60.                 }
  61.  
  62.                 return "Не_найдено";
  63.             }
  64.  
  65.             private void Serialize()
  66.             {
  67.                 File.WriteAllText(@"D:\serialization.json", this.FileName);
  68.             }
  69.  
  70.         }
  71.  
  72.         static void Main(string[] args)
  73.         {
  74.             FileReader writer = new FileReader();
  75.             string[] result = writer.ReadFile();
  76.  
  77.             if (result == null)
  78.             {
  79.                 return;
  80.             }
  81.  
  82.             Console.WriteLine("Элементов в строке: " + result.Length);
  83.             foreach (var item in result)
  84.             {
  85.                 Console.Write(item + " ");
  86.             }
  87.         }
  88.     }
  89. }
Advertisement
Add Comment
Please, Sign In to add comment