5

这段代码只是应该将一个字符串写入一个名为“all_results.txt”的文本文件中。我在 File.WriteAllText 中实现错误。在网上搜索解决方案后,我尝试使用 FileStream 和 StreamWriter 作为替代品。问题仍然存在。

它给了我:

IOException Unhandled:该进程无法访问文件“C:\Users\MadDebater\Desktop\ConsoleTest1\ConsoleTest\bin\Debug\all_results.txt”,因为它正被另一个进程使用。

奇怪的是,错误是任意发生的。它可能在第 3 次循环或第 45 次循环期间发生错误。我提供了该类的完整代码,以防问题比看起来更深。我确定这与我的病毒扫描程序或类似的东西无关。

try
                {
                    using (FileStream stream = new FileStream(@"all_results.txt", FileMode.Create)) // Exception here
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(result);
                        writer.Dispose();
                        writer.Close();
                    }

                    stream.Dispose();
                    stream.Close();
                }

                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex);
                }

即使我尝试这个,它仍然失败。

try
                {
                    File.WriteAllText(@"all_results.txt", result); // Exception here
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }

下面是该类的完整代码。它旨在获取 Twitter 推文列表,并使用贝叶斯分类对它们进行分类。

    using System;
    using System.IO;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using BayesClassifier;
    using System.Text.RegularExpressions;

    namespace ConsoleTest
    {
        class Analyzer
        {
            public static void Analyzing(List<string> all_results)
            {

            Reducting(all_results);
            Classifying();
        }

        public static void Reducting(List<string> all_results)
        {
            //Reductor
            //Precondition: List<string> results
            all_results.ForEach(delegate(String text)
            {

                const string ScreenNamePattern = @"@([A-Za-z0-9\-_&;]+)";
                const string HashTagPattern = @"#([A-Za-z0-9\-_&;]+)";
                const string HyperLinkPattern = @"(http://\S+)\s?";
                string result = text;

                if (result.Contains("http://"))
                {
                    var links = new List<string>();
                    foreach (Match match in Regex.Matches(result, HyperLinkPattern))
                    {
                        var url = match.Groups[1].Value;
                        if (!links.Contains(url))
                        {
                            links.Add(url);
                            result = result.Replace(url, String.Format(""));
                        }
                    }
                }

                if (result.Contains("@"))
                {
                    var names = new List<string>();
                    foreach (Match match in Regex.Matches(result, ScreenNamePattern))
                    {
                        var screenName = match.Groups[1].Value;
                        if (!names.Contains(screenName))
                        {
                            names.Add(screenName);
                            result = result.Replace("@" + screenName,
                               String.Format(""));
                        }
                    }
                }

                if (result.Contains("#"))
                {
                    var names = new List<string>();
                    foreach (Match match in Regex.Matches(result, HashTagPattern))
                    {
                        var hashTag = match.Groups[1].Value;
                        if (!names.Contains(hashTag))
                        {
                            names.Add(hashTag);
                            result = result.Replace("#" + hashTag,
                               String.Format(""));
                        }
                    }
                }

                // Write into text file
/*
                try
                {
                    using (FileStream stream = new FileStream(@"all_results.txt", FileMode.Create)) // Exception here
                {
                    using (StreamWriter writer = new StreamWriter(stream))
                    {
                        writer.WriteLine(result);
                        writer.Dispose();
                        writer.Close();
                    }

                    stream.Dispose();
                    stream.Close();
                }

                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex);
                }
                */
                try
                {
                    File.WriteAllText(@"all_results.txt", result); // Exception here
                }
                catch (IOException ex)
                {
                    Console.WriteLine(ex.Message);
                }

            });
        }

        public static void Classifying()
        {
            // Classifying

            BayesClassifier.Classifier m_Classifier = new BayesClassifier.Classifier();


            m_Classifier.TeachCategory("Positive", new System.IO.StreamReader("POSfile.txt"));
            m_Classifier.TeachCategory("Negative", new System.IO.StreamReader("NEGfile.txt"));

            Dictionary<string, double> newscore;
            newscore = m_Classifier.Classify(new System.IO.StreamReader("all_results.txt"));

            PrintResults(newscore);
}

        public static void PrintResults(Dictionary<string, double> newscore)
        {
            foreach (KeyValuePair<string, double> p in newscore)
            {
                Console.WriteLine(p.Key + ", " + p.Value);
            }

            List<string> list = new List<string>();
            using (StreamReader reader = new StreamReader("all_results.txt"))
            {
                string line;
                while ((line = reader.ReadLine()) != null)
                {
                    list.Add(line);          // Add to list.
                    Console.WriteLine(line); // Write to console.

                }

                reader.Close();
            }

            //PrintSentiment(newscore);
        }

        public static void PrintSentiment(Dictionary<string, double> newscore)
        {

            // if difference < 2, neutral
            // if neg < pos, pos
            // if pos < neg, neg

            double pos = newscore["Positive"];
            double neg = newscore["Negative"];
            string sentiment = "";

            if (Math.Abs(pos - neg) < 1.03)
            {
                sentiment = "NEUTRAL";
            }
            else
            {
                if (neg < pos)
                {
                    sentiment = "POSITIVE";
                }
                else if (pos < neg)
                {
                    sentiment = "NEGATIVE";
                }
            }

            Console.WriteLine(sentiment);


            // append tweet_collection to final_results <string> list
            // append sentiment tag to the final_results <string> list
            // recursive
        }
    }
}
4

10 回答 10

3

不要在 FileStream 和 StreamWriter 上调用 Dispose() 和 Close(),这将由 using 子句自动处理。

于 2010-09-27T13:42:04.327 回答
2

使用像 filemon 这样的实用程序来检查哪些进程正在使用该文件。

更新: 从我读到的过程监视器与 filemon 非常相似。从这些工具中的任何一个中,您都可以找到哪个进程在什么时候访问了您的文件。您可以在开始监控之前为您的文件添加过滤器。

您可以尝试的另一件事是锁定文件(如果存在)。

于 2010-09-27T12:53:37.880 回答
1

也许该文件是由病毒扫描程序或 Windows 索引服务访问的?

于 2010-09-27T12:54:37.937 回答
0

我在遇到类似问题时找到了该帖子。给定的建议给了我一个想法!所以为此我写了以下方法

public static void ExecuteWithFailOver(Action toDo, string fileName)
{

    for (var i = 1; i <= MaxAttempts; i++)
    {
        try
        {
            toDo();

            return;
        }
        catch (IOException ex)
        {
            Logger.Warn("File IO operation is failed. (File name: {0}, Reason: {1})", fileName, ex.Message);
            Logger.Warn("Repeat in {0} milliseconds.", i * 500);

            if (i < MaxAttempts)
                Thread.Sleep(500 * i);
        }
    }

    throw new IOException(string.Format(CultureInfo.InvariantCulture,
                                        "Failed to process file. (File name: {0})",
                                        fileName));

}

然后我按以下方式使用该方法

    Action toDo = () =>
                   {
                       if (File.Exists(fileName))
                           File.SetAttributes(fileName, FileAttributes.Normal);

                       File.WriteAllText(
                           fileName,
                           content,
                           Encoding.UTF8
                           );
                   };

    ExecuteWithFailOver(toDo, fileName);

后来分析日志我发现我的麻烦的原因是试图从并行线程对同一个文件采取行动。但是我仍然看到一些使用建议的故障转移方法的优点

于 2013-09-13T12:20:49.820 回答
0

尝试在文件操作周围使用锁定。 http://msdn.microsoft.com/en-us/library/c5kehkcz.aspx

于 2014-04-11T08:14:26.813 回答
0

尝试将 Thread.Sleep(1000) 放入循环中。就像上面提到的人一样,文件并不总是在循环的下一次迭代中及时发布。

于 2010-09-27T13:05:39.790 回答
0

佩德罗:

正如其他人所说,反复打开和关闭文件可能是问题所在。未提及的一种解决方案是在处理期间保持文件打开。完成后,可以关闭文件。

或者,或者,将您的文本收集到 StringBuilder 或其他一些内存文本存储中,然后在循环完成后将文本转储到文件中。

于 2010-09-27T17:36:44.770 回答
0

尝试将文件写入调试文件夹之外的另一个目录。

于 2010-09-27T12:56:53.437 回答
0

只是一个“狂野的镜头” - 如果您将文件放在更可预测的位置,例如 'c:\all_results.txt' 会有帮助吗?

于 2010-09-27T12:57:03.910 回答
0

正如其他人所说,反复打开和关闭文件可能是问题所在。未提及的一种解决方案是在处理期间保持文件打开。完成后,可以关闭文件。

于 2010-09-27T13:51:53.643 回答