FielStream类操作的是字节和节节数组,而stream类操作的是字符数据。字符数据易于使用,但是有些操作,比如随机文件访问(访问文件中间某点的数据),就必须由FileStream对象执行。
FielStream类维护内部文件指针,该指针指向文件中进行下一次读写操作的位置。
FileStream只能处理原始字节(raw byte),处理原去吧字节的功能使用FileStream类可以用于处理任何数据文件。
通过读取字节数据,FileStream对像可以用于读取图像和声音。代价是FileStream类不能将数据直接读入字符串
此指针可以修改用Seek方法,它有两个参数:
第一个参数:规定文件指针以字节为参数的移动距离
第二个参数:规定开始计算的起始位置
Code
1
byte[] byDataValue = new byte[200];
2
char[] charDataValue = new char[200];
3![](https://www.cnblogs.com/Images/OutliningIndicators/None.gif)
4![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
5
/// FilesStream读取用法
6
/// </summary>
7
private void FilesStreamReadFile()
8![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
9
try
10![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
11
FileStream fsFile = new FileStream(@"d:\log.cs", FileMode.Open);
12
//文件指针移到文件的135个字节
13
fsFile.Seek(135, SeekOrigin.Begin);
14
//将接下来的字节读到Array中
15
fsFile.Read(byDataValue, 0, 200);
16
}
17
catch (Exception e)
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
throw e;
20
}
21
//将字节转换成字符
22
Decoder dc = Encoding.UTF8.GetDecoder();
23
//字节数组转换成字符数组,便于显示
24
dc.GetChars(byDataValue, 0, byDataValue.Length, charDataValue, 0);
25![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
26
}
27
Code
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// FilesStream写入用法
3
/// </summary>
4
private void FileStreamWriteFile()
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
FileStream fsFile = new FileStream(@"D:\log.txt", FileMode.Create);
7
charDataValue="This is test string".ToCharArray();
8
byDataValue=new byte[charDataValue.Length];
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10
//将字符数组转换成字节数组
11
Encoder ec=Encoding.UTF8.GetEncoder();
12
ec.GetBytes(charDataValue,0,charDataValue.Length,byDataValue,0,true);
13![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
14
//将指针设定起始位置
15
fsFile.Seek(0,SeekOrigin.Begin);
16
//写入文件
17
fsFile.Write(byDataValue,0,byDataValue.Length);
18
}
19
FileStream对象通常会在StreamWriter或StreamReader内使用,并使用它们的方法处理文件。
Code
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// StreamWriter写入方法
3
/// </summary>
4
private void StreamWriterMetod()
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
try
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
FileStream fsFile = new FileStream(@"d:\log.txt",FileMode.OpenOrCreate);
9
StreamWriter swWriter = new StreamWriter(fsFile,Encoding.Default); //使用与系统一致的编码方式
10
//写入数据
11
swWriter.WriteLine("你好!");
13
swWriter.Close();
14
}
15
catch (Exception e)
16![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
17![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
18
throw e;
19
}
20
}
21
Code
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// StreamWriter读取方法
3
/// </summary>
4
private void StreamReaderMetod()
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
string sLine = "";
7
try
8![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
9
FileStream fsFile = new FileStream(@"d:\log.txt", FileMode.Open);
10
StreamReader srReader = new StreamReader(fsFile,,Encoding.Default);
11
//读取文件(读取大文件时,最好不要用此方法)
12
sLine = srReader.ReadToEnd();
13
txbValue.Text = sLine;
14
srReader.Close();
15
}
16
catch (Exception e)
17![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
18
throw e;
19
}
20
}
21
Code
1![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
/**//// <summary>
2
/// StreamWriter读取方法2
3
/// </summary>
4
private void StreamReaderMetodTwo()
5![ExpandedBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedBlockStart.gif)
{
6
try
7![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
8
FileStream fsFile = new FileStream(@"d:\log.txt", FileMode.Open);
9![](https://www.cnblogs.com/Images/OutliningIndicators/InBlock.gif)
10![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
/**//*
11
* 读取数据最简单的方法是Read()。此方法将流的下一个字符作为正整数值返回,
12
* 如果达到了流的结尾处,则返回-1。
13
*/
14
StreamReader srReader = new StreamReader(fsFile,Encoding.Default);
15
int iChar;
16
iChar = srReader.Read();
17
while (iChar != -1)
18![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
19
txbValue.Text += (Convert.ToChar(iChar));
20
iChar = srReader.Read();
21
}
22
srReader.Close();
23
}
24
catch (Exception e)
25![ExpandedSubBlockStart.gif](https://www.cnblogs.com/Images/OutliningIndicators/ExpandedSubBlockStart.gif)
{
26
throw e;
27
}
28
}
29