[英]Fastest way to trim text in all files
I have a function to trim all the text in all files in a directory. 我有一个函数来修剪目录中所有文件中的所有文本。 Here is the code
这是代码
var dbtables = System.IO.Directory.GetFiles(db);
foreach(var table in dbtables)
{
string text = File.ReadAllText(table);
File.WriteAllText(table, text.Trim());
}
There are many large files in this directory and it takes about 30 minutes to complete. 此目录中有许多大文件,大约需要30分钟才能完成。 Do you know of a faster way to do this?
你知道更快的方法吗?
Here's what I would recommend doing for each file: 这是我建议为每个文件做的事情:
Read the first character. 读第一个字符。
If it's whitespace, you have to rewrite the whole file. 如果它是空格,则必须重写整个文件。
If the first character is not whitespace, you won't be trimming the beginning, so you can just truncate the end. 如果第一个字符不是空格,则不会修剪开头,因此您可以截断结尾。
Your code currently trims whitespace from the beginning and end of each entire file, rather than each line. 您的代码当前从每个整个文件的开头和结尾修剪空白,而不是每行。 If you want to trim whitespace on a per-line basis, you could use:
如果要基于每行修剪空白,可以使用:
var dbtables = System.IO.Directory.GetFiles(db);
foreach (string table in dbtables)
{
string temp = table + ".tmp";
using (StreamWriter target = new StreamWriter(temp))
foreach (string line in File.ReadLines(table))
target.WriteLine(line.Trim());
File.Delete(table);
File.Move(temp, table);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.