当前位置:网站首页>C # method to read the text content of all files in the local folder
C # method to read the text content of all files in the local folder
2022-07-26 16:26:00 【A bowl of glutinous rice balls】
- Understand the test C# Read local txt Method of text content , stay Unity Read local files in the test ,txt The text encoding format is ANSI When , Chinese output is garbled ;txt The text encoding format is unicode、unicode big endian、utf-8 When , Chinese can be output normally .
Here are two kinds of reading txt Method of text content :( Remember to quote using System.IO)
string path = "C:\\Users\\hahaha\\Desktop\\test";
// Method 1
DirectoryInfo directoryInfo = new DirectoryInfo(path);
FileInfo[] files = directoryInfo.GetFiles();
for (int i = 0; i < files.Length; i++)
{
if(files[i].Extension.Equals(".txt")) // Judge whether it is txt file
{
string[] strs = File.ReadAllLines(path + "\\" + files[i].Name); // Full path of text file
for (int j = 0; j < strs.Length; j++)
{
Debug.Log("$$$$$$" + strs[j].ToString());
}
}
}
// Method 2
string[] strPath = Directory.GetFiles(path, "*.txt"); // Read only txt file , What you get is an all inclusive txt Character array of file path
foreach(string p in strPath)
{
string[] strs3 = File.ReadAllLines(p); // Content branch read
for (int j = 0; j < strs3.Length; j++)
{
Debug.Log("&&&&&&" + strs3[j].ToString());
}
string ss = File.ReadAllText(p); // Read all contents
Debug.Log("%%%" + ss);
foreach(var line in File.ReadLines(p)) // Content branch iterative reading
{
Debug.Log("@@@@" + line.ToString());
}
}
- In method 2 File The difference between the three reading methods :
(1)ReadAllLines and ReadAllText The difference between :
File.ReadAllLines() Returns an array of strings . Each string contains a line of the file .
File.ReadAllText() Returns a single string containing all lines of the file .
(2)ReadAllLines and ReadLines The difference between
File.ReadAllLines() Method reads the entire file at once and returns a string [] Array , Therefore, it takes time to use large files , Users are not recommended to wait , Until the whole array is returned .
File.ReadLines() Return to one IEnumerable< string> And it doesn't read the whole file at once , So when using large files , This is a better choice .
边栏推荐
- 【万字长文】使用 LSM-Tree 思想基于.Net 6.0 C# 实现 KV 数据库(案例版)
- 【E-MR】NameNode的错误恢复记录
- [ten thousand words long text] Based on LSM tree thought Net 6.0 C # realize kV database (case version)
- Summary of key knowledge of C language
- Bugku login2
- 【ARM学习(9) arm 编译器了解学习(armcc/armclang)】
- CAD进阶练习题(一)
- MVC和ECS两种设计架构的初浅理解
- 《From SICP to Lisp》视频回播
- 国元期货网上开户安全吗?开户办理流程是怎样的?
猜你喜欢
随机推荐
[tool sharing] automatic generation of file directory structure tool mddir
“核弹级” Log4j 漏洞仍普遍存在,并造成持续影响
2022牛客暑期多校训练营1(ACDGIJ)
博途PLC顺序开关机功能块(SCL)
spark-streaming状态流之mapWithState
PAT甲级 1044 Shopping in Mars
Happy 10th birthday, clojure
C # set different text watermarks for each page of word
PAT甲级 1045 Favorite Color Stripe
I would like to ask you guys, how to specify the character set of MySQL CDC tables? I can't find the corresponding connector parameters on the official website. I read one
2022牛客暑期多校训练营2(BDGHJKL)
[physical simulation] ultra simple shape matching simulates rigid body motion
Clojure 运行原理之字节码生成篇
There are six ways to help you deal with the simpledateformat class, which is not a thread safety problem
绘制漂亮的中学操场轮廓,生成带经纬度数据
2022年全国最新消防设施操作员(高级消防设施操作员)考试试题及答案
Activity之onCreate、onRestoreInstanceState恢复数据的区别
Linux Installation mysql8.0.29 detailed tutorial
2022 latest Beijing Construction Safety Officer simulation question bank and answers
研发效能的道与术 - 道篇








