【问题标题】:is there any better way to replace non-ascii chars in c#有没有更好的方法来替换 c# 中的非 ascii 字符
【发布时间】:2014-08-04 15:04:37
【问题描述】:

我有 C# 代码来删除传入文本文件中的非 ASCII 字符,然后输出到 .NonAsciiChars 文本文件。 因为传入的文件是 XML 格式,并且返回方法可能是 LF ONLY 或 CRLF,这就是为什么我不逐行进行替换(我正在使用 StreamReader.ReadToEnd())

现在的问题是当传入的文件很大(大约 2 GB)时,我收到以下错误。在我的案例中,有没有更好的方法来删除非 ASCII 字符?传入的文件也会发送 4GB 左右,恐怕到那时,读取部分也会得到 OutOfMemoryException。

非常感谢。

DateTime:2014-08-04 12:55:26,035 Thread ID:[1] Log Level:ERROR Logger Property:OS_fileParser.Program property:[(null)] - Message:System.OutOfMemoryException: Exception of type 'System.OutOfMemoryException' was thrown.
   at System.Text.StringBuilder.ExpandByABlock(Int32 minBlockCharCount)
   at System.Text.StringBuilder.Append(Char* value, Int32 valueCount)
   at System.Text.StringBuilder.Append(Char[] value, Int32 startIndex, Int32 charCount)
   at System.IO.StreamReader.ReadToEnd()
   at OS_fileParser.MyProgram.FormatXmlFile(String inFile) in D:\Test\myProgram.cs:line 530
   at OS_fileParser.MyProgram.Run() in D:\Test\myProgram.cs:line 336

myProgram.cs 第 530 行:content = Regex.Replace(content, pattern, "");

myProgram.cs 第 336 行:这是点调用以下方法

                const string pattern = @"[^\x20-\x7E]";

                string content;
                using (var reader = new StreamReader(inFile))
                {
                    content = reader.ReadToEnd();
                    reader.Close();
                }

                content = Regex.Replace(content, pattern, "");

                using (var writer = new StreamWriter(inFile + ".NonAsciiChars"))
                {
                    writer.Write(content);
                    writer.Close();
                }

                using (var myXmlReader = XmlReader.Create(inFile + ".NonAsciiChars", myXmlReaderSettings))
                {
                    try
                    {
                        while (myXmlReader.Read())
                        {
                        }
                    }
                    catch (XmlException ex)
                    {
                        Logger.Error("Validation error: " + ex);
                    }
                }

【问题讨论】:

  • 您的代码目前似乎可以运行,并且您正在寻求改进它。一般来说,这些问题对于本网站来说过于固执己见,但您可能会在CodeReview.SE 找到更好的运气。记得阅读their requirements,因为他们比这个网站更严格。
  • @gunr2171 谢谢你会尝试
  • @gunr2171 不,他当前的代码抛出错误when the incoming file is huge (around 2 GB) size。所以,它不起作用,如果它不起作用,它就属于 CodeReview 的题外话,属于这里。
  • @ANeves,同意。我的主要假设是该代码适用于较小的文件,但由于缺乏优化,它会阻塞较大的文件。这里很好。

标签: c# regex


【解决方案1】:

您将收到OutOfMemoryException。为了节省内存,您可以按部分处理文件,here 是如何逐行处理文件的一个很好的例子,here 是按字节,使用缓冲区(读取 1 个字节很慢)。

最简单的情况是这样的:

string line;    
using (var reader = new StreamReader(inFile))
    using (var writer = new StreamWriter(inFile + ".NonAsciiChars"))
        while ((line = reader.ReadLine()) != null)
        {
            ... // code to process line
            writer.Write(line);
        }

【讨论】:

  • 感谢您的建议,但是有时传入的文件只有两行,在读取文件行时,已经抛出 Outofmemory 错误,但我会尝试使用字节的建议 :) 谢谢
猜你喜欢
  • 1970-01-01
  • 2021-11-12
  • 2023-03-29
  • 1970-01-01
  • 2022-12-14
  • 1970-01-01
  • 1970-01-01
  • 2013-08-22
  • 1970-01-01
相关资源
最近更新 更多