C# File Streams Tutorial
C# File Streams Tutorial
FileMode – It specifies how to operation system should open the file. It has following members
1. Append - Open the file if exist or create a new file. If file exists then place cursor at the
end of the file.
2. Create - It specifies operating system to create a new file. If file already exists then
previous file will be overwritten.
3. CreateNew - It create a new file and If file already exists then throw IOException.
4. Open – Open existing file.
5. Open or Create – Open existing file and if file not found then create new file.
6. Truncate – Open an existing file and cut all the stored data. So the file size becomes 0.
In this programming Example we will create a new file "CsharpFile.txt" and saves it on disk.
And then we will open this file, saves some text in it and then close this file.
CREATE A BLANK .TXT FILE USING FILESTREAM
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.
8. namespace FileStream_CreateFile
9. {
11. {
13. {
15. fs.Close();
17. Console.ReadKey();
18. }
19. }
20. }
21. Output
22. File has been created and the Path is D:\\csharpfile.txt
23. _
Explanation:
In the above program I added System.IO namespace so that I could use FileStream class in my
program. Then I created an object of FileStream class fs to create a
new csharpfile.txt in D drive.
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.
8. namespace AccessFile
9. {
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. FileStream fs = new FileStream("D:\\
csharpfile.txt", FileMode.Append);
15. byte[] bdata=Encoding.Default.GetBytes("Hello
File Handling!");
16. fs.Write(bdata, 0, bdata.Length);
17. fs.Close();
18. Console.WriteLine("Successfully saved file with
data : Hello File Handling!");
19. Console.ReadKey();
20. }
21. }
22. }
Output
Explanation
In the above program again I created object as fs of FileStrem class. Then Encoded a string
into bytes and kept into byte[] variable bdata and finally using Write() method of
FileStream stored string into file.
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.
8. namespace FileStream_ReadFile
9. {
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. string data;
15. FileStream fsSource = new FileStream("D:\\
csharpfile.txt", FileMode.Open, FileAccess.Read);
16. using (StreamReader sr = new
StreamReader(fsSource))
17. {
18. data = sr.ReadToEnd();
19. }
20. Console.WriteLine(data);
21. Console.ReadLine();
22. }
23. }
24. }
Output
_
Explanation
In the above example I opened file in a Read permission and use StreamReader class to read
file.
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.
8. namespace StreamWriter_Class
9. {
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. string file = @"D:\csharpfile.txt";
15. using (StreamWriter writer = new
StreamWriter(file))
16. {
17. writer.Write("Hello");
18. writer.WriteLine("Hellow StreamWriter
Class");
19. writer.WriteLine("How are you!");
20. }
21. Console.WriteLine("Data Saved Successfully!");
22. Console.ReadKey();
23. }
24. }
25. }
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.
8. namespace StreamWriter_VariableData
9. {
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. string file = @"D:\csharpfile.txt";
15. int a, b, result;
16. a = 5;
17. b = 10;
18. result = a + b;
19.
20. using (StreamWriter writer = new
StreamWriter(file))
21. {
22. writer.Write("Sum of {0} + {1} = {2}", a,
b, result);
23. }
24. Console.WriteLine("Saved");
25. Console.ReadKey();
26. }
27. }
28. }
Now open the D:\csharpfile.txt again and you will see the following text in a file.
Sum of 5 + 10 = 15
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.
8. namespace StreamReader_Class
9. {
10. class Program
11. {
12. static void Main(string[] args)
13. {
14. string file = @"D:\csharpfile.txt";
15.
16. //Writer data to text file
17. using (StreamWriter writer = new
StreamWriter(file))
18. {
19. writer.WriteLine("This tutorial explains
how to use StreamReader Class in C# Programming");
20. writer.WriteLine("Good Luck!");
21. }
22.
23. //Reading text file using StreamReader Class
24. using (StreamReader reader = new
StreamReader(file))
25. {
26. Console.WriteLine(reader.ReadToEnd());
27. }
28. Console.ReadKey();
29.
30. }
31. }
32. }
Output:
Good Luck!