-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
72 lines (61 loc) · 2.05 KB
/
Program.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace ConsoleApp_FileIO
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("File IO\n");
string filename = "MyFile.txt";
string next_file = "MyFile2.txt";
string currentDir = Environment.CurrentDirectory;
// Write to file
File.WriteAllText("MyFile.txt", "BMW;750L" + Environment.NewLine);
File.AppendAllText("MyFile.txt", "Yugo;205 SS" + Environment.NewLine);
// Read from file
string read = File.ReadAllText(filename);
List<string> fileData = File.ReadAllLines(filename).ToList();
// Create file [with StreamWriter] and write text
using (StreamWriter sw = File.CreateText(next_file))
{
sw.WriteLine("VW;Polo");
sw.WriteLine("Volvo;S60");
}
List<Car> cars = new List<Car>();
string filePath = currentDir + "\\" + filename;
bool fileExist = File.Exists(filePath);
if (fileExist)
{
using(StreamReader sr = File.OpenText(next_file))
{
string s;
while ((s = sr.ReadLine()) != null)
{
string[] carArray = s.Split(";");
Car newCar = new Car(carArray[0], carArray[1]);
cars.Add(newCar);
}
}
Console.WriteLine($"All Cars in file {filename}:\n");
foreach (Car car in cars)
{
Console.WriteLine(car.Brand + " - " + car.Model);
}
}
Console.ReadLine();
}
}
class Car
{
public Car(string brand, string model)
{
Brand = brand;
Model = model;
}
public string Brand { get; set; }
public string Model { get; set; }
}
}