先来代码再解释
public Worker(string path)
{
FileStream fs = new FileStream(
path,
FileMode.OpenOrCreate,
FileAccess.Write,
FileShare.Read,
20480,
FileOptions.RandomAccess); string hello = "hello ";
string word = "word"; byte[] b = Encoding.ASCII.GetBytes(hello);
byte[] w = Encoding.ASCII.GetBytes(word); fs.Seek(b.LongLength + 2, SeekOrigin.Begin);
fs.Write(w, 0, w.Length); fs.Seek(0, SeekOrigin.Begin);
fs.Write(b, 0, b.Length); fs.Close(); }
其中比较关键的是 Seek 函数的使用 ,微软给出的解释 将该流的当前位置设置为给定值
第二个值也很关键 SeekOrigin 一个枚举类型,
public enum SeekOrigin
{
// 摘要:
// 指定流的开头。
Begin = 0,
//
// 摘要:
// 指定流内的当前位置。
Current = 1,
//
// 摘要:
// 指定流的结尾。
End = 2,
}
Current 这个值的意思不是很理解,知道的亲们,劳烦解释一下。
当然,这个例子只是随机写入的,随机读取的也就是把函数名换成 Read ,别的几乎都是一模一样的.