【问题标题】:Write to file cut off写入文件被切断
【发布时间】:2011-04-12 19:52:09
【问题描述】:

我的目标是获取一个句子文件,应用一些基本过滤,然后将剩余的句子输出到一个文件和终端。我正在使用 Hunspell 库。

这是我从文件中获取句子的方法:

    public static string[] sentencesFromFile_old(string path)
    {
        string s = "";
        using (StreamReader rdr = File.OpenText(path))
        {
            s = rdr.ReadToEnd();
        }
        s = s.Replace(Environment.NewLine, " ");
        s = Regex.Replace(s, @"\s+", " ");
        s = Regex.Replace(s, @"\s*?(?:\(.*?\)|\[.*?\]|\{.*?\})", String.Empty);
        string[] sentences = Regex.Split(s, @"(?<=\. |[!?]+ )");
        return sentences;
    }

这是写入文件的代码:

        List<string> sentences = new List<string>(Checker.sentencesFromFile_old(path));
        StreamWriter w = new StreamWriter(outFile);
        foreach(string x in xs)
            if(Checker.check(x, speller))
            {
                w.WriteLine("[{0}]", x);
                Console.WriteLine("[{0}]", x);
            }

这是检查器:

    public static bool check(string s, NHunspell.Hunspell speller)
    {
        char[] punctuation = {',', ':', ';', ' ', '.'};
        bool upper = false;
        // Check the string length.
        if(s.Length <= 50 || s.Length > 250)
            return false;
        // Check if the string contains only allowed punctuation and letters.
        // Also disallow words with multiple consecutive caps.
        for(int i = 0; i < s.Length; ++i)
        {
            if(punctuation.Contains(s[i]))
                continue;
            if(Char.IsUpper(s[i]))
            {
                if(upper)
                    return false;
                upper = true;
            }
            else if(Char.IsLower(s[i]))
            {
                upper = false;
            }
            else return false;
        }
        // Spellcheck each word.
        string[] words = s.Split(' ');
        foreach(string word in words)
            if(!speller.Spell(word))
                return false;
        return true;
    }

句子在终端上打印得很好,但是文本文件在 2015 个字符处截断了中间句子。这是怎么回事?

编辑:当我删除 check 方法的某些部分时,文件在 2000 或 4000 左右的不同长度处被截断。删除拼写检查完全消除了截断。

【问题讨论】:

    标签: c# file-io hunspell


    【解决方案1】:

    您需要在关闭流之前刷新它。

    w.Flush();
    w.Close();
    

    using 语句(您也应该使用)将自动关闭流,但不会刷新它。

    using( var w = new StreamWriter(...) )
    {
      // Do stuff
      w.Flush();
    }
    

    【讨论】:

    • Np,我个人认为明确关闭流的自然行为也应该刷新它。不知道他们为什么决定不那样做。
    • 不需要显式刷新 - 但您确实需要显式关闭。只是关闭会自动刷新它。问题是目前没有任何事情可以关闭作者。
    • @Paul 在 ILSpy 中检查 StreamWriter,看起来 Flush 将在 Dispose 上调用。无论如何,在 close 之前调用 Flush 肯定不会有什么坏处。
    • 希望这是真的。我经历了几十个在关闭之前必须刷新流的错误。
    • @rsbarro:它会损害代码的可读性,IMO ......尤其是如果你想刷新,即使抛出异常。看到一个(无意中)误导性的答案被接受是一种耻辱:(只是一个using 声明是一个更好的方法。
    【解决方案2】:

    你写完后要关闭StreamWriter吗?你可以试试这样的:

    using(StreamWriter w = new StreamWriter(outFile))
    {
        foreach(string x in xs)
        {
            if(Checker.check(x, speller))
            {
                w.WriteLine("[{0}]", x);
                Console.WriteLine("[{0}]", x);
            }
        }
    }
    

    using 语句将在内部代码执行完毕后关闭StreamWriter(通过在StreamWriter 上调用Dispose 方法)。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-07-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多