当前位置:网站首页>Introduction to FileStream class of C #
Introduction to FileStream class of C #
2022-07-25 04:37:00 【Chen Yan will act】
FileStream class
FileStream( File stream ) This class is mainly used for reading and writing binary files , You can also use it to read and write any file .
StreamReader( Stream reader ) and StreamWriter( Stream writer ) Dedicated to reading and writing text files
Use FileStream Read write binary :FileStream Instance is used to read and write data in a file , To construct FileStream example , You need to provide the following 4 In the information :
- File to access
Generally, the full pathname of a file is provided - Mode indicating how to open the file
Create a new file or open an existing file , If you open an existing file , The write operation is to overwrite the original contents of the file , Or append to the end of the file ? - Indicates how to access the file
read-only 、 Just write 、 Or read and write - share access
Indicates whether to have exclusive access to the file , If other streams are allowed to access files at the same time , Then these streams are read-only Just write Or read and write files
Arguments to the constructor :
- FileMode( Turn on mode ) Append,Create,CreateNew,Open,OpenOrCreate and Truncate
- FileAccess( Read or write ) Read,ReadWrite and Write
- FileShare( File sharing settings ) Delete,Inheritable,None,Read,ReadWrite and Write
PS:
If the file doesn't exist Append Open and Truncate It throws an exception , If the file exists CreateNew It throws an exception ; Create and OpenOrCreate:Create Will delete existing files , Create a new empty file ,OpenOrCreate It will judge whether there are files currently , If not, it will create ;
matters needing attention :
When we finish using a stream , Be sure to call fs.Close(); Method to close the flow , Closing the flow frees the resources associated with it , Allow other applications to set up streams for the same file . This operation will also flush the buffer .
Sample code
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)) // Verify that the file exists
{
File.Delete(path);
}
// Create and write files
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);
}
// Read the file
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();
}
}
}
边栏推荐
- 运筹学基础【一】 之 导论
- Etcd learning
- 如何取得数据库创建时间?
- Unity3d learning note 9 - loading textures
- Preparation for Android development in big companies
- After the failure of listing in Hong Kong, tuba rabbit terminated its gem IPO, and the founder responded
- 在开发或调试IP直接方案时需要注意Host的值跟直接的IP要一致
- 市场是对的
- Penetration test target combat SQL injection getshell
- In the Internet of things market, Bosch sensor has launched a number of new solutions
猜你喜欢

暗黑王者|ZEGO 低照度图像增强技术解析

Analytic hierarchy process of MATLAB

2019 telecast retest test questions

Grafana visual configuration diagram histogram

Network engineering case: integrated network design of CII company

IT自媒体高调炫富,被黑客组织盯上,铁定要吃牢饭了…
![[detailed tutorial] a thorough article on mongodb aggregation query](/img/81/1ac7afa778849b8a4b103107fd9cb6.png)
[detailed tutorial] a thorough article on mongodb aggregation query

GDT,LDT,GDTR,LDTR

Custom dialog (including header and footer)

【浅析STM32之GPIO寄存器(CRL/CRH)配置 】
随机推荐
PHP Baidu qianqianhua installment API
Paper:《Peeking Inside the Black Box: Visualizing Statistical Learning with Plots of Individual Condi
Huawei | mlgoperf: ML boot inline for optimizing performance
Apipost signs up with Chinatelecom! Work together to accelerate the digital transformation of enterprises
MySQL -- index and transaction isolation level
[cloud picture theory] 248 illustrated public network domain name resolution: easy domain name access to websites / mailboxes
深入掌握Pod
C# 之 FileStream类介绍
【基于stm32f103的SHT30温湿度显示】
市场是对的
ESWC 2018 | R-GCN:基于图卷积网络的关系数据建模
Function and technical principle of data desensitization [detailed explanation]
Wechat applet experiment case: simple idiom dictionary
Grafana visual configuration diagram histogram
自然的状态最好
LVGL 8.2 Spinbox
mitt.js:小型事件发布订阅库
Dark king | analysis of zego low illumination image enhancement technology
Millet 100W fast charging, 50W wireless charging technology exposure! Oppo Shen Yiren responded: boring!
SMIC's revenue in 2018 was $3.36 billion, and 14nm technology was mass produced this year