zoukankan      html  css  js  c++  java
  • C# 随机读写入文件

    先来代码再解释

         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 ,别的几乎都是一模一样的.

  • 相关阅读:
    JOIN中的外连接(external join)
    将流数据输出到Mysql中
    updataStateByKey算子的使用
    RDD算子的使用
    sparkstreaming 黑名单过滤
    sparkSQL中的example学习(3)
    sparkSQL中的example学习(1)
    sparkSQL中的example学习(2)
    shuffle调优
    回形取数
  • 原文地址:https://www.cnblogs.com/cnryb/p/3170528.html
Copyright © 2011-2022 走看看