当前位置:网站首页>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 )
边栏推荐
- 力扣27. 移除元素
- How to apply for SSL certificate from the manufacturer
- Lambda&Stream
- Approaching history, introduction to the London Guard Museum
- MySQL query gadget (I) replace a property value of the object in the JSON array in the JSON format string field
- 圆心科技,很焦虑?
- 深度学习------不同方法实现Inception-10
- Unity3d Google Earth
- Sailing experience not to be missed in New York Tourism: take you to enjoy the magnificent city scenery from different perspectives
- amd锐龙CPU A320系列主板如何安装win7
猜你喜欢

力扣704. 二分查找

Unity packaging failure solution

A virtual reality secret room escape adventure, let you see Technology Singapore

Wildcard SSL certificate issuing time

【Paper】2021_ Uniformity of heterogeneous hybrid multi-level intelligent systems using UGV and UAV

SCM learning notes: interrupt learning

LXC 和 LXD 容器总结
Sourcetree usage

【Paper】2021_ Observer-Based Controllers for Incrementally Quadratic Nonlinear Systems With Disturbanc

Detailed explanation of the process of "flyingbird" small game (camera adjustment and following part)
随机推荐
MySQL查询小工具(一)json格式的字符串字段中,替换json数组中对象的某个属性值
JPA复合主键使用
Qos(Quality of Service)
Efficiency test of adding and querying ArrayList and LinkedList
Lambda&Stream
LxC and LXD container summary
【Paper】2013_ An efficient model predictive control scheme for an unmanned quadrotor helicopter
How to renew an SSL certificate
Introduction to some representations, neighbors and degrees of Graphs
Circle center technology, very anxious?
Is the Flink connector JDBC open source? Where can I download it
Steamvr causes abnormal scene camera
[control] multi agent system summary. 4. control agreement.
Unity/ue reads OPC UA and OPC Da data (UE4)
[UAV] gyroscope data analysis, taking Victor intelligent jy901b as an example
Implementation of one interview question one distributed lock every day
Check London attractions suitable for parents and children in winter vacation
PBR material: basic principle and simple fabrication
How does unity use mapbox to implement real maps in games?
Universal Studios Singapore: a good place for a one-day parent-child tour in Singapore