string textContent = fileStream.ReadToEnd();byte[] bytes = System.Text.Encoding.Default.GetBytes(textContent);
字符数据易于使用, 但是有些操作,比如随机文件访问(访问文件中间某点的数据),就必须由FileStream对象执行.
其中创建FileStream对象最简单的构造函数如下:
1
FileStream file
=
new
FileStream(fileName,FileMode.Member);
2 FileStream file = new FileStream(fileName, FileMode.Member, FileAccess.Member);
2 FileStream file = new FileStream(fileName, FileMode.Member, FileAccess.Member);
而FileAccess的成员:
成 员 | 说 明 |
Read | 打开文件,用于只读 |
Write | 打开文件,用于只写 |
ReadWrite | 打开文件,用于读写 |
对文件进行不是FileAccess枚举成员指定的操作会导致抛出异常。此属性的作用是,基于用户的身份验证级别改变用户对文件的访问权限。
在FileStream构造函数不使用FileAccess枚举参数的版本中,使用默认值FileAccess. ReadWrite。
FileMode枚举成员,使用每个值会发生什么,取决于指定的文件名是否表示已有的文件。
成 员 | 文 件 存 在 | 文件不存在 |
Append | 打开文件,流指向文件的末尾,只能与枚举FileAccess.Write联合使用 | 创建一个新文件。只能与枚举FileAccess.Write联合使用 |
Create | 删除该文件,然后创建新文件 | 创建新文件 |
CreateNew | 抛出异常 | 创建新文件 |
Open | 打开现有的文件,流指向文件的开头 | 抛出异常 |
OpenOrCreate | 打开文件,流指向文件的开头 | 创建新文件 |
Truncate | 打开现有文件,清除其内容。流指向文件的开头,保留文件的初始创建日期 | 抛出异常 |
StreamWriter允许将字符和字符串写入文件,它处理底层的转换,向FileStream对象写入数据。StreamReader也类似。
实例:
1
using
System;
2 using System.Data;
3 using System.IO;
4 using System.Text;
5
6 /**/ /// <summary>
7/// Summary description for FileReadAndWrite
8/// </summary>
9 public class FileReadAndWrite
10 {
11 public FileReadAndWrite()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16 }
17 /**//// <summary>
18 /// 用FileStream写文件
19 /// </summary>
20 /// <param name="str"></param>
21 /// <returns></returns>
22 public void FileStreamWriteFile(string str)
23 {
24 byte[] byData;
25 char[] charData;
26 try
27 {
28 FileStream nFile = new FileStream("love.txt", FileMode.Create);
29 //获得字符数组
30 charData = str.ToCharArray();
31 //初始化字节数组
32 byData = new byte[charData.Length];
33 //将字符数组转换为正确的字节格式
34 Encoder enc = Encoding.UTF8.GetEncoder();
35 enc.GetBytes(charData, 0, charData.Length,byData,0,true);
36 nFile.Seek(0, SeekOrigin.Begin);
37 nFile.Write(byData, 0, byData.Length);
38 }
39 catch (Exception ex)
40 {
41 throw ex;
42 }
43 }
44 /**//// <summary>
45 /// FileStream读取文件
46 /// </summary>
47 /// <param name="filePath"></param>
48 /// <returns></returns>
49 public string FileStreamReadFile(string filePath)
50 {
51 byte[] data = new byte[100];
52 char[] charData = new char[100];
53 try
54 {
55 FileStream file = new FileStream(filePath, FileMode.Open);
56 //文件指针指向0位置
57 file.Seek(0, SeekOrigin.Begin);
58 //读入两百个字节
59 file.Read(data, 0, 200);
60 //提取字节数组
61 Decoder dec = Encoding.UTF8.GetDecoder();
62 dec.GetChars(data, 0, data.Length, charData, 0);
63 }
64 catch (Exception ex)
65 {
66 throw ex;
67 }
68 return Convert.ToString(charData);
69 }
70 /**//// <summary>
71 /// StreamWriter写文件
72 /// </summary>
73 public void StreamWriterWriteFile()
74 {
75 try
76 {
77 FileStream nFile = new FileStream("love.txt", FileMode.CreateNew);
78 StreamWriter writer = new StreamWriter(nFile);
79
80 writer.WriteLine("I love You!");
81 writer.WriteLine("Do you love me!");
82 writer.Close();
83 }
84 catch
85 { }
86 }
87 /**//// <summary>
88 /// StreamReader读取文件
89 /// </summary>
90 /// <returns></returns>
91 public string StreamReaderReadFile()
92 {
93 string str="";
94 try
95 {
96 FileStream file = new FileStream("love.txt", FileMode.Open);
97 StreamReader sr = new StreamReader(file);
98 while (sr.ReadLine()!=null)
99 {
100 str += sr.ReadLine();
101 }
102 //或者str = sr.ReadToEnd();
103 sr.Close();
104 }
105 catch
106 { }
107 return str;
108 }
109}
110
2 using System.Data;
3 using System.IO;
4 using System.Text;
5
6 /**/ /// <summary>
7/// Summary description for FileReadAndWrite
8/// </summary>
9 public class FileReadAndWrite
10 {
11 public FileReadAndWrite()
12 {
13 //
14 // TODO: Add constructor logic here
15 //
16 }
17 /**//// <summary>
18 /// 用FileStream写文件
19 /// </summary>
20 /// <param name="str"></param>
21 /// <returns></returns>
22 public void FileStreamWriteFile(string str)
23 {
24 byte[] byData;
25 char[] charData;
26 try
27 {
28 FileStream nFile = new FileStream("love.txt", FileMode.Create);
29 //获得字符数组
30 charData = str.ToCharArray();
31 //初始化字节数组
32 byData = new byte[charData.Length];
33 //将字符数组转换为正确的字节格式
34 Encoder enc = Encoding.UTF8.GetEncoder();
35 enc.GetBytes(charData, 0, charData.Length,byData,0,true);
36 nFile.Seek(0, SeekOrigin.Begin);
37 nFile.Write(byData, 0, byData.Length);
38 }
39 catch (Exception ex)
40 {
41 throw ex;
42 }
43 }
44 /**//// <summary>
45 /// FileStream读取文件
46 /// </summary>
47 /// <param name="filePath"></param>
48 /// <returns></returns>
49 public string FileStreamReadFile(string filePath)
50 {
51 byte[] data = new byte[100];
52 char[] charData = new char[100];
53 try
54 {
55 FileStream file = new FileStream(filePath, FileMode.Open);
56 //文件指针指向0位置
57 file.Seek(0, SeekOrigin.Begin);
58 //读入两百个字节
59 file.Read(data, 0, 200);
60 //提取字节数组
61 Decoder dec = Encoding.UTF8.GetDecoder();
62 dec.GetChars(data, 0, data.Length, charData, 0);
63 }
64 catch (Exception ex)
65 {
66 throw ex;
67 }
68 return Convert.ToString(charData);
69 }
70 /**//// <summary>
71 /// StreamWriter写文件
72 /// </summary>
73 public void StreamWriterWriteFile()
74 {
75 try
76 {
77 FileStream nFile = new FileStream("love.txt", FileMode.CreateNew);
78 StreamWriter writer = new StreamWriter(nFile);
79
80 writer.WriteLine("I love You!");
81 writer.WriteLine("Do you love me!");
82 writer.Close();
83 }
84 catch
85 { }
86 }
87 /**//// <summary>
88 /// StreamReader读取文件
89 /// </summary>
90 /// <returns></returns>
91 public string StreamReaderReadFile()
92 {
93 string str="";
94 try
95 {
96 FileStream file = new FileStream("love.txt", FileMode.Open);
97 StreamReader sr = new StreamReader(file);
98 while (sr.ReadLine()!=null)
99 {
100 str += sr.ReadLine();
101 }
102 //或者str = sr.ReadToEnd();
103 sr.Close();
104 }
105 catch
106 { }
107 return str;
108 }
109}
110