Home
Manage Your Code
Snippet: Using Stream Reader And Stream Writer (C#)
Title: Using Stream Reader And Stream Writer Language: C#
Description: Using Stream Reader And Stream Writer Views: 2183
Author: Raghu k Date Added: 10/11/2007
Copy Code  
1Namespace
2------------
3using System.IO;
4
5
6// *** Read from file ***

7--------------------------
8
9// Specify file, instructions, and privelegdes

10file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Read);
11
12// Create a new stream to read from a file

13StreamReader sr = new StreamReader(file);
14
15// Read contents of file into a string

16string s = sr.ReadToEnd();
17
18// Close StreamReader

19sr.Close();
20
21// Close file

22file.Close();
23
24
25
26// *** Write to file ***

27--------------------------
28
29// Specify file, instructions, and privelegdes

30FileStream file = new FileStream("test.txt", FileMode.OpenOrCreate, FileAccess.Write);
31            
32// Create a new stream to write to the file

33StreamWriter sw = new StreamWriter(file);
34
35// Write a string to the file

36sw.Write("Hello file system world!");
37
38// Close StreamWriter

39sw.Close();
40
41// Close file

42file.Close();
43
44
45
46
47
48
49
50
51
52
Notes
Found Here: ----------- http://www.devhood.com/tutorials/tutorial_details.aspx?tutorial_id=400