当前位置:网站首页>C# 之 FileStream类介绍
C# 之 FileStream类介绍
2022-07-25 04:08:00 【陈言必行】
FileStream类
FileStream(文件流) 这个类主要用于二进制文件中读写,也可以使用它读写任何文件。
StreamReader(流读取器)和StreamWriter(流写入器)专门用于读写文本文件
使用FileStream读写二进制文件:FileStream实例用于读写文件中的数据,要构造FileStream实例,需要提供下面的4中信息:
- 要访问的文件
一般提供一个文件的完整路径名 - 表示如何打开文件的模式
新建文件或打开一个现有文件,如果打开一个现有的文件,写入操作是覆盖文件原来的内容,还是追加到文件的末尾? - 表示访问文件的方式
只读、只写、还是读写 - 共享访问
表示是否独占访问文件,如果允许其他流同时访问文件,则这些流是只读 只写 还是读写文件
构造函数的参数:
- FileMode( 打开模式) Append,Create,CreateNew,Open,OpenOrCreate和Truncate
- FileAccess(读取还是写入) Read,ReadWrite和Write
- FileShare(文件共享设置) Delete,Inheritable,None,Read,ReadWrite和Write
PS:
如果文件不存在Append Open和Truncate会抛出异常, 如果文件存在 CreateNew会抛出异常; Create和 OpenOrCreate:Create会删除现有的文件,新建一个空的文件,OpenOrCreate会判断当前是否有文件,没有的话才会创建;
注意事项:
当我们使用完了一个流之后,一定要调用fs.Close();方法去关闭流,关闭流会释放与它相关联的资源,允许其他应用程序为同一个文件设置流。这个操作也会刷新缓冲区。
示例代码
using System;
using System.IO;
using System.Text;
namespace CSharpDemo
{
class Programma
{
static void Main(string[] args)
{
string path = @"E:\MyTest.txt";
if (File.Exists(path)) // 校验文件是否存在
{
File.Delete(path);
}
// 创建并写入文件
using (FileStream fs_write = File.Create(path))
{
byte[] info = new UTF8Encoding(true).GetBytes("123");
fs_write.Write(info, 0, info.Length);
byte[] info1 = new UTF8Encoding(true).GetBytes("456789");
fs_write.Write(info1, 0, info1.Length);
}
// 读取文件
using (FileStream fs_read = File.OpenRead(path))
{
byte[] byteArr = new byte[1024];
UTF8Encoding coding = new UTF8Encoding(true);
while (fs_read.Read(byteArr, 0, byteArr.Length) > 0)
{
Console.WriteLine(coding.GetString(byteArr));
}
}
Console.ReadKey();
}
}
}
边栏推荐
- MySQL 中RDS 链接数突然上涨怎么查?
- Infinite connection · infinite collaboration | the first global enterprise communication cloud conference WECC is coming
- DIY can decorate the mall system, you can also have!
- Millet 100W fast charging, 50W wireless charging technology exposure! Oppo Shen Yiren responded: boring!
- Es- retrieve the selected field from the search
- Divide candy Huawei od JS
- Which stock exchange has the lowest commission? Is online account manager safe to open an account
- Application of AI in testing
- Unity3d learning note 9 - loading textures
- LVGL 8.2 Tabview
猜你喜欢

Unity 之 UPR优化建议汇总

Eve-ng lab simulator Cisco, H3C test host alias

To clarify the tax arrears: there is no tax arrears, and will continue to operate in compliance, rooted in China

Maker concept design to adapt to popular education

Preparation for Android development in big companies

Do you really understand images? (machine vision)

Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises

Pandora IOT development board learning (RT thread) - Experiment 16 WiFi module experiment (learning notes)

Student thinking vs workplace thinking

EMQ Yingyun technology was successfully selected into the 2022 "cutting edge 100" list of Chinese entrepreneurs
随机推荐
@ResponseBody注解的总结
Millet 100W fast charging, 50W wireless charging technology exposure! Oppo Shen Yiren responded: boring!
RGB and SATA function switching module based on Quanzhi rk3568j
Opencv4.5.x+cuda11.0.x source code compilation and yolov5 acceleration tutorial!
@Summary of ResponseBody annotation
Apache Doris series: in depth understanding of real-time analytical database Apache Doris
Deeply understand the connection state and reliable mechanism of TCP protocol
Definition and basic terms of tree
What causes the wait event of TCP socket (kgas) in oracle?
Understand Huawei's 2018 financial report with one picture
Digital collections can go further without hype
Sony announced the closure of Beijing mobile phone factory! The production line will be moved to Thailand, and the cost can be reduced by half!
01 create project warehouse
Interpretation and download of the report | ink Tianlun July database industry report, be prepared for danger in times of safety, and safety first
Metinfo function public function getcity() error: XXX function no permission load!!!
6.7 billion dollars! The acquisition of IDT by Renesas Electronics was finally approved!
Chart is code: build a new generation of graphics library in a coded way -- feekin
[interview must brush 101] greedy algorithm, simulation, string
Jenkins continues to integrate entry to mastery
Application of AI in testing