zoukankan      html  css  js  c++  java
  • 写文件 指定固定开始位置

      private void button1_Click(object sender, EventArgs e)
            {
                string filename = "";
                string strFile = "";
                int index = 0;
                do
                {
                    int fileId = index++;
                    filename = string.Format("{0}.dat", Convert.ToString(fileId).PadLeft(4, '0'));
                    strFile = DbFunc.getConnectionWZ() + filename;
                }
                while (File.Exists(strFile));
     
                // 打开文件 ()  , 或通过 File 创建立如:

                FileStream fss = new FileStream(strFile, FileMode.CreateNew);
                // 转换为字节 写入数据 ( 可写入中文 )
                Byte[] info = new UTF8Encoding(true).GetBytes("This is some text in the file.");
                // 字节数组 , 字节偏移量 , 最多写入的字节数
                BinaryWriter   w   =   new   BinaryWriter(fss);
                // 设置要写入的偏移量 
                fss.Position = 13;// fss.Length;
                fss.Write(info, 0, info.Length);   //这个也可以 


                Byte[] info1 = new UTF8Encoding(true).GetBytes("测试第二个");
                // 字节数组 , 字节偏移量 , 最多写入的字节数
                BinaryWriter w1 = new BinaryWriter(fss);
                // 设置要写入的偏移量 
                fss.Position = 73;// fss.Length;
                fss.Write(info1, 0, info1.Length);   //这个也可以
                w.Close();
                fss.Close();

            }

  • 相关阅读:
    GDI+重绘笔记
    VC++动态链接库(DLL)编程深入浅出(四)
    VC++动态链接库(DLL)编程深入浅出(三)
    VC++动态链接库(DLL)编程深入浅出(二)
    VC++动态链接库(DLL)编程深入浅出(一)
    AOP在 .NET中的七种实现方法
    [c#菜鸟]lambda表达式
    C# IP地址与整数之间的转换
    log4net在xp下不能写日志到mdb,报错Could not load type 'System.Security.Claims.ClaimsIdentity'
    Active Directory 域服务之AD DS 逻辑组件概述
  • 原文地址:https://www.cnblogs.com/liufei88866/p/1797964.html
Copyright © 2011-2022 走看看