C#中using的使用-以FileStream写入文件为例

mac2022-06-30  22

场景

CS中FileStream的对比以及使用方法:

https://blog.csdn.net/BADAO_LIUMANG_QIZHI/article/details/100396022

关注公众号霸道的程序猿获取编程相关电子书、教程推送与免费下载。

实现

将创建文件流对象的过程写在using中,会自动的帮助我们释放流所占用的资源。

新建命令行程序:

//使用FileStream来写入数据 using (FileStream fsWrite = new FileStream(@"C:\Users\Administrator\Desktop\badao.txt",FileMode.OpenOrCreate,FileAccess.Write)) { string str = "关注公众号:霸道的程序猿,霸道的程序猿,霸道的程序猿,霸道的程序猿"; byte[] buffer = Encoding.Default.GetBytes(str); fsWrite.Write(buffer,0,buffer.Length); } Console.WriteLine("写入成功"); Console.ReadKey();

 

运行效果

 

转载于:https://www.cnblogs.com/badaoliumangqizhi/p/11449378.html

最新回复(0)