当前位置:网站首页>[C # notes] reading and writing of the contents of text files
[C # notes] reading and writing of the contents of text files
2022-07-07 02:20:00 【Start()】
Mainly used two API:
System.IO.StreamReader.ReadToEnd() // Stream reader Read the whole content of the text
System.IO.StreamWriter.Write(string value) // Stream writer Write text content to file
// Read
public string ReadTxt(string txtPath)
{
StreamReader streamReader = new StreamReader(txtPath, Encoding.Default);
string con = streamReader.ReadToEnd();
streamReader.Close();
return con;
}
// write in
/// <param name="txtPath"> File path </param>
/// <param name="writeCon"> Write content </param>
/// <param name="isCoverCon"> Whether to cover the content </param>
public void WriteTxt(string txtPath, string writeCon, bool isCoverCon = false)
{
#region If there is no text file , Just create one
if (!File.Exists(txtPath))
File.Create(txtPath).Close();
#endregion
if (!isCoverCon)
writeCon += ReadTxt(filePath);
StreamWriter streamWriter = new StreamWriter(txtPath);
streamWriter.Write(writeCon);// The content will completely cover the original text content
streamWriter.Close();
}
A newly discovered simple method , Let's record :
Use C# Encapsulated two API:
System.IO.File.ReadAllText(string path, Encoding encoding); // Read
System.IO.File.WriteAllText(string path, string contents, Encoding encoding); // write in
边栏推荐
- Lidar: introduction and usage of ouster OS
- 传感器:DS1302时钟芯片及驱动代码
- Data connection mode in low code platform (Part 1)
- NPM install compilation times "cannot read properties of null (reading 'pickalgorithm')“
- 【服务器数据恢复】raid损坏导致戴尔某型号服务器崩溃的数据恢复案例
- leetcode:5. 最长回文子串【dp + 抓着超时的尾巴】
- Centros 8 installation MySQL Error: The gpg Keys listed for the "MySQL 8.0 Community Server" repository are already ins
- Recent applet development records
- Collection recommandée!! Quel plug - in de gestion d'état flutter est le plus fort? Regardez le classement des manons de l'île, s'il vous plaît!
- 遇到慢SQL该怎么办?(下)
猜你喜欢
ROS learning (23) action communication mechanism
Flir Blackfly S 工业相机:配置多个摄像头进行同步拍摄
Vingt - trois mille feuilles? "Yang mou" derrière l'explosion de la consommation végétale
Flir Blackfly S工业相机:颜色校正讲解及配置与代码设置方法
Batch delete data in SQL - set in entity
argo workflows源码解析
centos8安裝mysql報錯:The GPG keys listed for the “MySQL 8.0 Community Server“ repository are already ins
Infrared camera: juge infrared mag32 product introduction
微服务架构介绍
Analyze "C language" [advanced] paid knowledge [II]
随机推荐
Web开发小妙招:巧用ThreadLocal规避层层传值
Threadlocalutils (tool class IV)
ROS learning (24) plugin
Lumion 11.0 software installation package download and installation tutorial
将截断字符串或二进制数据
企业中台建设新路径——低代码平台
Several classes and functions that must be clarified when using Ceres to slam
阿里云中间件开源往事
豆瓣平均 9.x,分布式领域的 5 本神书!
Stm32f4 --- general timer update interrupt
Flir Blackfly S 工业相机:自动曝光配置及代码
CISP-PTE之命令注入篇
What to do when encountering slow SQL? (next)
Analyze "C language" [advanced] paid knowledge [II]
The last line of defense of cloud primary mixing department: node waterline design
传感器:DS1302时钟芯片及驱动代码
UC伯克利助理教授Jacob Steinhardt预测AI基准性能:AI在数学等领域的进展比预想要快,但鲁棒性基准性能进展较慢
[unique] what is the [chain storage structure]?
大咖云集|NextArch基金会云开发Meetup来啦!
BigDecimal 的正确使用方式