当前位置:网站首页>Operation file file class method
Operation file file class method
2022-06-30 04:57:00 【LintonL】
File operations
File.ReadAllText(“path”,Encoding.Default);// Read all text return string
File.ReadAllBytes(“path”);// Read the file , return byte[], Treat files as binary .
l===========================================
File.WriteAllLines(“path”,new string[4] ,Encoding.Default);// take string Array writes to file by line .
File.WriteAllText(“path”,“string”);// Write all strings to file
File.WriteAllBytes(“path”,newbyte[5]);// take byte[] Write all to file
File.AppendAllText()// take string Append to file
FileStream fs=File.OpenRead();// Returns a read-only FileStream
FileStream fs=File.OpenWrite();// Return write only FileStream
FileStream fs=new FileStream( Parameters );
Stream( The parent of all streams , Is an abstract class .)
The classes of file operations are all in System.IO.*;
adopt FileStream Write read text file . For Chinese, if every time you read 1 individual byte What's the effect ? Better choice →StreamReader
Use FileStream Read text file .
Use FileStream Make large file copies .
File.ReadAllBytes() And File.WriteAllBytes() Copy files .( View memory usage )
Use using It can easily release resources ( Automatically call Dispose Method )
Only when IDispose Interface can be used using Release resources
use StreamWriter Can simplify the text type Stream To deal with
StreamWriter Is the auxiliary Stream Processed
using (StreamWriter writer = new StreamWriter(stream, encoding))
{
writer.WriteLine(" Hello ");
}
and StreamWriter similar , StreamReader Simplified reading of text type streams .
Stream stream = File.OpenRead("c:/1.txt");
using (StreamReader reader = new StreamReader(stream,encoding))
{
//Console.WriteLine(reader.ReadToEnd());
Console.WriteLine(reader.ReadLine());
}
ReadToEnd Used to read from the current position to the end , If the content is large, it will occupy memory ; Every call goes down , Cannot be inadvertently called twice
ReadLine Read a line , If it comes to the end , Then return to null.
Binary negation for positive numbers , add 1 Is the binary of negative numbers .
Encoding.Default
System.Text.DBCSCodePageEncoding Double byte character set . If the output EncodingName Words , The result is : Simplified Chinese (gb2312)
Encoding.Default: Current with the operating system “ Regional and language options ” of .
If no code is provided in the system , You can use the GetEncoding(" The encoded value string represents ") Get the code .
//Encodingencoding=Encoding.GetEncoding("gb2312");
StringBuildersb =newStringBuilder();
EncodingInfo[]infos = System.Text.Encoding.GetEncodings();
for(inti = 0; i < infos.Length; i++)
{
sb.Append(infos[i].CodePage +" "+ infos[i].DisplayName +" "+ infos[i].Name +"\r\n");
}
File.WriteAllText("encodings.txt",sb.ToString());
original text :
http://blog.csdn.net/n1g2q2012/article/details/9380911
Similar reference :
http://www.cnblogs.com/zysbk/archive/2012/08/09/2629589.html ( recommend , Graphic version )
边栏推荐
- Redis implements SMS login function (I) traditional session login
- The most comprehensive summary notes of redis foundation + advanced project in history
- 【Paper】2021_ Analysis of the Consensus Protocol of Heterogeneous Agents with Time-Delays
- 图的一些表示方式、邻居和度的介绍
- brew安装nvm报nvm command not found解决方案
- pycharm 数据库工具
- Bean creation process and lazy init delay loading mechanism
- Keywords implements and @override
- 力扣周赛293题解
- Unity realizes rotation and Revolution
猜你喜欢
Approaching history, introduction to the London Guard Museum
Oracle-数据的基本操作
Qos(Quality of Service)
Why does the computer have no network after win10 is turned on?
力扣27. 移除元素
Create a simple battle game with photon pun
How to repair expired SSL certificates?
Malignant bug: 1252 of unit MySQL export
Unity3d Google Earth
Create transfer generation point
随机推荐
National Museum of Singapore - give you spiritual and physical satisfaction
One interview question every day to talk about the process of TCP connection and disconnection
harbor api 2.0查询
Unity3d realizes Google Digital Earth
[UAV] kinematic analysis from single propeller to four rotor UAV
Arrays class
【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV
Bean creation process and lazy init delay loading mechanism
z-index属性在什么情况下会失效?
Recommended cultural landmarks of these tourist attractions in Bangkok
Brew install NVM command not found solution
Foreign SSL certificate
2021-03-16
A collection of errors encountered in machine learning with unity
Exploration of unity webgl
[control] multi agent system summary. 1. system model. 2. control objectives. 3. model transformation.
Solution to the 292 week match of Li Kou
How to install win7 on AMD Ruilong CPU A320 series motherboard
Efficiency test of adding and querying ArrayList and LinkedList
【Paper】2015_ Active fault-tolerant control system design with trajectory re-planning against actuator