写入文件并读取bfile的文件流

我有一个Web服务,该服务检查字典以查看文件是否存在,然后如果存在,则读取文件,否则将其保存到文件中.这是来自网络应用程序.我想知道什么是最好的方法,因为如果同时访问同一文件,我偶尔会收到FileNotFoundException异常.
这是代码的相关部分:

String signature;
signature = "FILE," + value1 + "," + value2 + "," + value3 + "," + value4;     // this is going to be the filename

string result;            
MultipleRecordset mrSummary = new MultipleRecordset();  // MultipleRecordset is an object that retrieves data from a sql server database

if (mrSummary.existsFile(signature))
{                
    result = mrSummary.retrieveFile(signature);                
}
else
{                
    result = mrSummary.getMultipleRecordsets(System.Configuration.ConfigurationManager.ConnectionStrings["MyConnectionString"].ConnectionString.ToString(), value1, value2, value3, value4);
    mrSummary.saveFile(signature, result);
}

以下是查看文件是否已存在的代码:

private static Dictionary<String, bool> dict = new Dictionary<String, bool>();

public bool existsFile(string signature)
{                       
    if (dict.ContainsKey(signature))
    {
        return true;
    }
    else
    {
        return false;
    }                                
}

如果它已经存在,这是我用来检索的内容:

try
{                               

    byte[] buffer;
    FileStream fileStream = new FileStream(@System.Configuration.ConfigurationManager.AppSettings["CACHEPATH"] + filename, FileMode.Open, FileAccess.Read, FileShare.Read);
      try
      {
        int length = 0x8000;  // get file length
        buffer = new byte[length];            // create buffer
        int count;                            // actual number of bytes read
        JSONstring = "";
        while ((count = fileStream.Read(buffer, 0, length)) > 0)
        {
            JSONstring += System.Text.ASCIIEncoding.ASCII.GetString(buffer, 0, count);
        }
      }
      finally
      {
        fileStream.Close();
      }
}
catch (Exception e)
{

    JSONstring = "{\"error\":\"" + e.ToString() + "\"}";                
}

如果该文件先前不存在,则将JSON保存到文件中:

try 
{                
    if (dict.ContainsKey(filename) == false)
    {
        dict.Add(filename, true);
    }
    else
    {
        this.retrieveFile(filename, ipaddress);
    }

}
catch
{


}


try
{
    TextWriter tw = new StreamWriter(@System.Configuration.ConfigurationManager.AppSettings["CACHEPATH"] + filename);
    tw.WriteLine(JSONstring);
    tw.Close();
}
catch { 

}

以下是我有时通过运行上述代码而得到的异常的详细信息:

System.IO.FileNotFoundException: Could not find file 'E:\inetpub\wwwroot\cache\FILE,36,36.25,14.5,14.75'.
File name: 'E:\inetpub\wwwroot\cache\FILE,36,36.25,14.5,14.75'
   at System.IO.__Error.WinIOError(Int32 errorCode, String maybeFullPath)
   at System.IO.FileStream.Init(String path, FileMode mode, FileAccess access, Int32 rights, Boolean useRights, FileShare share, Int32 bufferSize, FileOptions options, SECURITY_ATTRIBUTES secAttrs, String msgPath, Boolean bFromProxy)
   at System.IO.FileStream..ctor(String path, FileMode mode, FileAccess access, FileShare share)
   at com.myname.business.MultipleRecordset.retrieveFile(String filename, String ipaddress)

解决方法:

之所以收到FileNotFoundException,是因为您在实际写入文件之前将文件名添加到了字典中.因此,对同一文件的并发操作将导致此问题.

写入后将其添加到字典中只会产生一个新问题:它将开始尝试同时写入文件(并且失败很惨).如果性能至关重要,我可以将字典更改为Dictionary< string,object>.并将该值用作文件级别的同步对象.您还需要添加一个单独的同步对象,以检查并添加到字典本身.

但是,这可能是多余的,并且需要使用Monitor.Enter和Monitor.Exit手动进行.这是一个稍微简单的实现:

static HashSet<string> files = new HashSet<string>();
static object syncRoot = new object();

void Whatever(string filename, string ipaddress)
{
    bool fileFound;

    lock (syncRoot)
    {
        fileFound = files.Contains(filename);
        if (!fileFound)
        {
            files.Add(filename);
            // Code to write file here
        }
    }

    if (fileFound)
    {
        retrieveFile(filename, ipaddress);
    }
}

完成写操作后,性能会稍差一些,因为它会阻塞所有读操作,直到写操作完成.如果大多数操作都是读取的,那么这不是问题.

在此示例中,我将您的字典更改为HashSet,因为您似乎仅在使用键而不是值.

上一篇:c#-我应该何时处理文件,何时应该逐行读取文件?


下一篇:Mysql 学习随笔