blob: c5f4f8f909321d94bd344d2e948c88f95c28358a (
plain)
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
|
# Reading and Writing Files
To open a file for reading or writing, we can use `System.IO.File`.
```cs
using (var stream = File.OpenRead("path\\to\\file.csv"))
{
}
using (var stream = File.OpenWrite("path\\to\\file.csv"))
{
}
```
These both return a `FileStream` for working with our file. Since our data is text, we will need to use a `StreamReader` and `StreamWriter` to read and write the text.
```cs
using (var stream = File.OpenRead("path\\to\\file.csv"))
using (var reader = new StreamReader(stream))
{
}
using (var stream = File.OpenWrite("path\\to\\file.csv"))
using (var writer = new StreamWriter(stream))
{
}
```
`StreamReader` and `StreamWriter` have shortcuts for doing this.
```cs
using (var reader = new StreamReader("path\\to\\file.csv"))
{
}
using (var writer = new StreamWriter("path\\to\\file.csv"))
{
}
```
CsvHelper doesn't know anything about your encoding, so if you have a specific encoding, you'll need to specify that in your stream.
```cs
using (var reader = new StreamReader("path\\to\\file.csv", Encoding.UTF8))
{
}
using (var writer = new StreamWriter("path\\to\\file.csv", Encoding.UTF8))
{
}
```
`CsvReader` and `CsvWriter` take a `TextReader` and `TextWriter` in their constructors. `TextReader` and `TextWriter` are `abstract` classes for reading and writing text. `StreamReader` inherits `TextReader` and `StreamWriter` inherits `TextWriter`, so we can use those with `CsvReader` and `CsvWriter`.
```cs
using (var reader = new StreamReader("path\\to\\file.csv"))
using (var csv = new CsvReader(reader))
{
}
using (var writer = new StreamWriter("path\\to\\file.csv"))
using (var csv = new CsvWriter(writer))
{
}
```
|