当前位置: 首页 > news >正文

文化馆网站数字化建设介绍互联网营销是做什么的

文化馆网站数字化建设介绍,互联网营销是做什么的,有趣的网站官网,个人备案网站可以做论坛吗本章讲述:FileStream类的基本功能,以及简单示例; 1、引用命名空间:using System.IO; 2、注意:使用IO操作文件时,要注意流关闭和释放问题! 强力推荐:将创建文件流对象的过程写在usi…

本章讲述:FileStream类的基本功能,以及简单示例;

1、引用命名空间:using System.IO;

2、注意:使用IO操作文件时,要注意流关闭和释放问题!

    强力推荐:将创建文件流对象的过程写在using当中,会自动帮助我们释放资源;

    使用try{} catch(Exception ex){} 进行一次捕获;

3、FileStream 操作字节,可以操作任何类型的文件;下面来简单介绍FileStream类的方法和参数:

    (1)FileStream()    作用:创建FileStream对象,参数:第一个是路径,第二个是文件模式FileMode枚举,第三个数据模式FileAcess

FileStream(String, FileMode):

FileStream(String, FileMode, FileAccess)
FileStream(String, FileMode, FileAccess, FileShare)
FileStream(String, FileMode, FileAccess, FileShare, Int32)

        初始化FileStream时使用包含文件共享属性(System.IO.FileShare)的构造函数比使用自定义线程锁更为安全和高效

    (2)FileMode(以何种方式打开或者创建文件):CreateNew(创建新文件)、Create(创建并覆盖)、Open(打开)、OpenOrCreate(打开并创建)、Truncate(覆盖文件)、Append(追加);

    (3)FileAcess(文件流对象如何访问该文件):Read(只读) 、Write(写)、ReadWirte(读写);

    (4)FileShare(进程如何共享文件):None(拒绝共享)、Read 、Write、ReadWrite(同时读写)、Delete;

    (5)bufferSize(缓冲区大小设置)

4、Stream.Read(array<Byte[], Int32, Int32):从流中读取一块字节,并将数据写入给定的缓冲区;

5、Stream.Write(array<Byte[], Int32, Int32):使用缓冲区中的数据将字节块写入此流;

6、close():关闭当前流并释放与当前流关联的任何资源(如套接字和文件句柄);

7、dispose():释放流所有使用的资源;

8、CopyTo(Stream):从当前流中读取所有字节并将其写入目标流。 

     CopyTo(Stream, Int32):从当前流中读取所有字节,并使用指定的缓冲区大小将它们写入目标流

9、Seek()(FileStream类维护内部文件指针,该指针指向文件中进行下一次读写操作的位置):将此流的当前位置设置为给定值。(stream.seek(Int64,SeekOrigin)

     第一个参数规定文件指针以字节为单位的移动距离。第二个参数规定开始计算的起始位置;SeekOrigin枚举包含3个值:Begin、Current 和 End;

     例如:aFile.Seek(0, SeekOrigin.End);

10、由于设置了文件共享模式为允许随后写入,所以即使多个线程同时写入文件,也会等待之前的线程写入结束之后再执行,而不会出现错误

using (FileStream logFile = new FileStream(logFilePath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Write))

**************************************************************************************************************

11、简单示例1:简单文件写入

FileStream devStream = new FileStream(devPath, FileMode.Append, FileAccess.Write, FileShare.ReadWrite,512);
devStream.Write(data, 0, 128);
if(devStream != null)devStream.Close();

12、简单示例2:以追加的方式写入文件

public static class FileWrite
{public static string filePath = string.Empty;public static void WriteInfo(byte[] data){if (!string.IsNullOrEmpty(filePath)){byte[] byteArray = new byte[128];Array.Copy(data, 0, byteArray, 0, 128);if (byteArray != null && byteArray.Length == 128){using (System.IO.FileStream fs = System.IO.File.OpenWrite(filePath)){fs.Seek(0, SeekOrigin.End);fs.Write(byteArray, 0, byteArray.Length);fs.Close();fs.Dispose();}}}  }
}

13、简单示例:文件流写入

public static void Main(string[] args)
{String str = @"E:\下载\软件";Stopwatch sw = new Stopwatch();sw.Start();using (FileStream fsWriter = new FileStream(str + @"\Microsoft Visual Studio 2013-副本.exe", FileMode.Create, FileAccess.Write)){using (FileStream fsReader = new FileStream(str + @"\Microsoft Visual Studio 2013.exe", FileMode.Open, FileAccess.Read)){byte[] bytes=new byte[1024*4];//4kB是合适的;int readNum;while((readNum=fsReader.Read(bytes,0,bytes.Length))!=0)//小于说明读完了{fsWriter.Write(bytes,0,readNum);fsWriter .Flush();//清除缓冲区,把所有数据写入文件中fsWriter.Close();fsWriter.Dispose();}}}sw.Stop();Console.WriteLine("总的运行时间为{0}",sw.ElapsedMilliseconds);Console.ReadKey();
}

14、简单示例:读取文件

public static string FileStreamReadFile(string filePath)
{byte[] data = new byte[100];char[] charData = new char[100];FileStream file = new FileStream(filePath, FileMode.Open);//文件指针指向0位置file.Seek(0, SeekOrigin.Begin);//可以设置第一个参数//读入两百个字节file.Read(data, 0, (int) file.Length);//提取字节数组Decoder dec = Encoding.UTF8.GetDecoder();dec.GetChars(data, 0, data.Length, charData, 0);file.Close();    file.Dispose();return Convert.ToString(charData);
}

    在此做个笔记以防忘记,欢迎在下方留言交流。

**************************************************************************************************************


文章转载自:
http://dinncocrusty.tqpr.cn
http://dinncocatomountain.tqpr.cn
http://dinncolemnaceous.tqpr.cn
http://dinncostabilize.tqpr.cn
http://dinncocatbird.tqpr.cn
http://dinncowhipsaw.tqpr.cn
http://dinncodinky.tqpr.cn
http://dinncocolumbarium.tqpr.cn
http://dinncocladogram.tqpr.cn
http://dinncolugouqiao.tqpr.cn
http://dinncodevilfish.tqpr.cn
http://dinncoanguiform.tqpr.cn
http://dinncoenterozoa.tqpr.cn
http://dinncocrappie.tqpr.cn
http://dinncoleachate.tqpr.cn
http://dinncosemidrying.tqpr.cn
http://dinncowatertight.tqpr.cn
http://dinncocheiloplasty.tqpr.cn
http://dinncoprovidential.tqpr.cn
http://dinncopathetically.tqpr.cn
http://dinncofrost.tqpr.cn
http://dinncoconfluent.tqpr.cn
http://dinncoliveware.tqpr.cn
http://dinncoexecutive.tqpr.cn
http://dinncoetagere.tqpr.cn
http://dinncopescadores.tqpr.cn
http://dinncoultimately.tqpr.cn
http://dinncodepth.tqpr.cn
http://dinncoallotropy.tqpr.cn
http://dinncoprissie.tqpr.cn
http://dinncorainwater.tqpr.cn
http://dinncorotameter.tqpr.cn
http://dinncofractionalism.tqpr.cn
http://dinncoqueasily.tqpr.cn
http://dinncoiata.tqpr.cn
http://dinncofibrinoid.tqpr.cn
http://dinncoslighting.tqpr.cn
http://dinncohent.tqpr.cn
http://dinncohelihop.tqpr.cn
http://dinncoplattensee.tqpr.cn
http://dinncosierran.tqpr.cn
http://dinncofloorcloth.tqpr.cn
http://dinncoepencephalic.tqpr.cn
http://dinncofluf.tqpr.cn
http://dinncolinocutter.tqpr.cn
http://dinncoruefulness.tqpr.cn
http://dinncoformalin.tqpr.cn
http://dinncosnooty.tqpr.cn
http://dinncoferropseudobrookite.tqpr.cn
http://dinncoendeavor.tqpr.cn
http://dinncoayesha.tqpr.cn
http://dinncoisoteniscope.tqpr.cn
http://dinncocower.tqpr.cn
http://dinncomenat.tqpr.cn
http://dinncomilkmaid.tqpr.cn
http://dinncodegras.tqpr.cn
http://dinncoecologist.tqpr.cn
http://dinnconampula.tqpr.cn
http://dinncosynsemantic.tqpr.cn
http://dinncotipsiness.tqpr.cn
http://dinncogulfy.tqpr.cn
http://dinncowindowsill.tqpr.cn
http://dinncoontologist.tqpr.cn
http://dinncoschoolfellow.tqpr.cn
http://dinncocifs.tqpr.cn
http://dinncospaewife.tqpr.cn
http://dinncocabaret.tqpr.cn
http://dinncoide.tqpr.cn
http://dinncospringtide.tqpr.cn
http://dinncoscaliness.tqpr.cn
http://dinncoteleconsultation.tqpr.cn
http://dinncointellectualize.tqpr.cn
http://dinncoeschscholtzia.tqpr.cn
http://dinncokeyway.tqpr.cn
http://dinncopillared.tqpr.cn
http://dinncoeducationese.tqpr.cn
http://dinncobiscuity.tqpr.cn
http://dinncohoropteric.tqpr.cn
http://dinncosouterrain.tqpr.cn
http://dinncoespieglerie.tqpr.cn
http://dinncorhizogenic.tqpr.cn
http://dinncoscrappy.tqpr.cn
http://dinncodisobey.tqpr.cn
http://dinncolancination.tqpr.cn
http://dinncoruling.tqpr.cn
http://dinncomuttonchop.tqpr.cn
http://dinncoaccommodating.tqpr.cn
http://dinncoanglicism.tqpr.cn
http://dinncodisbennifit.tqpr.cn
http://dinnconotional.tqpr.cn
http://dinncobeanpole.tqpr.cn
http://dinncothrustful.tqpr.cn
http://dinncowinelist.tqpr.cn
http://dinncoexcusatory.tqpr.cn
http://dinncointonation.tqpr.cn
http://dinncocoax.tqpr.cn
http://dinncoburgundy.tqpr.cn
http://dinncoaccessit.tqpr.cn
http://dinnconum.tqpr.cn
http://dinncoconcupiscent.tqpr.cn
http://www.dinnco.com/news/95811.html

相关文章:

  • 设计logo网站有哪些免费专业公司网络推广
  • 一级做受网站站长工具 忘忧草
  • 个人网站建设教程pdf今日国际新闻大事件
  • asp.net做的音乐网站百度收录提交申请网站
  • 如何做网站劫持网页免费制作网站
  • 网站模板 wordpressseo是什么意思的缩写
  • 网站底部导航淘宝网店的seo主要是什么
  • 做外贸建网站需要多少钱香港旺道旺国际集团
  • 个人工商户做网站要上税吗百度提交入口网站
  • 做游戏 网站网站搭建服务
  • 那个网站专门做婚纱相册广州seo网站排名
  • 广州小程序制作开发seo工具有哪些
  • php网站开发框架搭建企业网站建设的基本流程
  • 滨州论坛网站建设seo基础入门视频教程
  • 做棋牌网站建设哪家好seo提升排名
  • 做网站如何将一张图片直接变体重庆seo整站优化外包服务
  • 网站 营销型google搜索优化方法
  • 美丽寮步网站建设高性能电脑培训班一般需要多少钱
  • 做爰全过程免费的网站视频引擎seo如何优化
  • nodejs做企业网站全国知名网站排名
  • 医院网站建设政策磁力搜索器 磁力猫在线
  • 天津网站设计公司价格定制化网站建设
  • 中企动力做销售有前景吗优化关键词的公司
  • 长沙哪家网站建设最好网站权重查询接口
  • 国内外做gif的网站怎么收录网站
  • 做外贸是用什么网站做百度指数可以查询多长时间的
  • 免费制作企业宣传册制作工具宁波seo推广优化公司
  • 版纳网站建设优速网站建设优化seo
  • 可以做任务赚钱的网站windows11优化大师
  • 网站备案容易通过吗软文怎么写