zoukankan      html  css  js  c++  java
  • C# FileStream 对象的Seek()方法

    C# FileStream 对象的Seek()方法

    2010年12月08日 11:22:00 kevin617 阅读数 7565

    FileStream 可以随机读写文件 使用 Seek 方法

    Seek()  ----------有两个参数 第一参数规定文件指针以字节为单位移动的距离。第二个参数规定开始计算的位置

    用 SeekOrigin 枚举的一个值表示 : SeekOrigin 有三个值:Begin  Current   End。

    aFile.Seek(8,SeekOrigin.Begin)---------将文件的指针移动到第八个字节。起始位置就是文件的第一个字节。

    aFile.Seek(-5,SeekOrigin.End); 从文件的末尾向前查找五个字节。。

    aFile.Seek(2,SeekOrigin.Current);

    以下来自MSDN

    FileStream.Seek 方法

    .NET Framework 2.0

    其他版本

    将该流的当前位置设置为给定值。

    命名空间:System.IO
    程序集:mscorlib(在 mscorlib.dll 中)

    语法

    public override long Seek (
    	long offset,
    	SeekOrigin origin
    )
    
    范例:
    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);
            }
        }
    }
    
    延伸阅读:Windows Mobile上实现断点续传
  • 相关阅读:
    今日SGU 5.2
    奇异值分解(SVD)小结
    计蒜客16495 Truefriend(fwt)
    计蒜客16492 building(二分线段树/分块)
    hihocoder 1323 回文字符串(字符串+dp)
    hihocoder 1320 压缩字符串(字符串+dp)
    hdu6121 build a tree(树)
    hdu6103 Kirinriki(trick+字符串)
    hdu6097 Mindis(几何)
    hdu 6057 Kanade's convolution(子集卷积)
  • 原文地址:https://www.cnblogs.com/grj001/p/12224830.html
Copyright © 2011-2022 走看看