简体   繁体   English

修剪所有文件中文本的最快方法

[英]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: 这是我建议为每个文件做的事情:

  1. Read the first character. 读第一个字符。

  2. If it's whitespace, you have to rewrite the whole file. 如果它是空格,则必须重写整个文件。

    1. Read the whole file into memory. 将整个文件读入内存。
    2. Iterate character-by-character from the beginning to find the first non-whitespace character. 从头开始逐个字符迭代,找到第一个非空白字符。
    3. Iterate character-by-character from the end to find the first non-whitespace character. 从末尾迭代逐个字符以找到第一个非空白字符。
    4. Seek to the beginning of the file. 寻找文件的开头。
    5. Write just the middle characters of your string to the file. 只将字符串的中间字符写入文件。
    6. Set the file's length to the number of characters you wrote. 将文件的长度设置为您编写的字符数。
  3. If the first character is not whitespace, you won't be trimming the beginning, so you can just truncate the end. 如果第一个字符不是空格,则不会修剪开头,因此您可以截断结尾。

    1. Read the file backwards, character-by-character, until you hit a character that isn't whitespace. 逐个字符地向后读取文件,直到找到不是空格的字符。
    2. If you've found whitespace, set the file's length to its current length minus the number of whitespace characters. 如果找到了空格,请将文件的长度设置为当前长度减去空白字符数。

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM