本文整理汇总了C#中System.IO.FileStream.Seek方法的典型用法代码示例。如果您正苦于以下问题:C# FileStream.Seek方法的具体用法?C# FileStream.Seek怎么用?C# FileStream.Seek使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.IO.FileStream
的用法示例。
在下文中一共展示了FileStream.Seek方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: Main
//引入命名空间
using System;
using System.IO;
class FStream
{
static void Main()
{
const string fileName = "Test#@@#.dat";
// Create random data to write to the file.
byte[] dataArray = new byte[100000];
new Random().NextBytes(dataArray);
using(FileStream
fileStream = new FileStream(fileName, FileMode.Create))
{
// Write the data to the file, byte by byte.
for(int i = 0; i < dataArray.Length; i++)
{
fileStream.WriteByte(dataArray[i]);
}
// Set the stream position to the beginning of the file.
fileStream.Seek(0, SeekOrigin.Begin);
// Read and verify the data.
for(int i = 0; i < fileStream.Length; i++)
{
if(dataArray[i] != fileStream.ReadByte())
{
Console.WriteLine("Error writing data.");
return;
}
}
Console.WriteLine("The data was written to {0} " +
"and verified.", fileStream.Name);
}
}
}
示例2: Main
//引入命名空间
using System;
using System.IO;
public class FSSeek
{
public static void Main()
{
long offset;
int nextByte;
// alphabet.txt contains "abcdefghijklmnopqrstuvwxyz"
using (FileStream fs = new FileStream(@"c:\temp\alphabet.txt", FileMode.Open, FileAccess.Read))
{
for (offset = 1; offset <= fs.Length; offset++)
{
fs.Seek(-offset, SeekOrigin.End);
Console.Write(Convert.ToChar(fs.ReadByte()));
}
Console.WriteLine();
fs.Seek(20, SeekOrigin.Begin);
while ((nextByte = fs.ReadByte()) > 0)
{
Console.Write(Convert.ToChar(nextByte));
}
Console.WriteLine();
}
}
}
输出:
zyxwvutsrqponmlkjihgfedcba uvwxyz