none
流读取文件发生“系统资源不足,无法完成请求的服务” RRS feed

  • 问题

  • 我写了一个文件类用于保存和读取自定义的文件,可是如果自定义文件体积如果大于6X兆字节的时候,读取时必发生“系统资源不足,无法完成请求的服务”的错误提示。

    我不认为6X兆字节是很大的文件,也不认为系统真的资源不足。我的文件类可能存在问题,但是我努力了几天也没有发现不对劲的地方。

    我使用文件类的方式是:

    foreach(string file in filePaths)
    {
    	try
    	{
    		using(FileStream fs=new FileStream(file,FileMode.Open, FileAccess.ReadWrite,FileShare.None,4096,true))
    		{
    			MyFileRWriter mfw=new MyFileRWriter(fs);
    		}
    	}
    	catch(Exception ex)
    	{
    	}
    }
    每次在调用构造函数“MyFileRWriter mfw=new MyFileRWriter(fs)”,且目标文件的体积大于6X兆字节时,在函数的结尾处就会引发“系统资源不足……”的提示


    我的文件类是:

    class MyFileRWriter : IDisposable
    {
        // Fields
        private string authorName;
        public FileExceptionDel exceptionDel;
        private string fileComment;
        private string fileNameExtension;
        private DateTime fileTime;
        private string pattern;
        private int fileVersion;
        private byte[] rawData;
    
        // Nested Types
        public delegate void FileExceptionDel(string res, string msg);
    
        // Properties
        public string Comment
        {
            get
            {
                return this.fileComment;
            }
            set
            {
                this.fileComment = value;
            }
        }
    
        public byte[] DataBytes
        {
            get
            {
                return this.rawData;
            }
            set
            {
                this.rawData = value;
            }
        }
    
        public FileExceptionDel ExceptionDel
        {
            get
            {
                return this.exceptionDel;
            }
            set
            {
                this.exceptionDel = value;
            }
        }
    
        public string AuthorName
        {
            get
            {
                return this.authorName;
            }
            set
            {
                this.authorName = value;
            }
        }
    
        public string FileNameExtension
        {
            get
            {
                return this.fileNameExtension;
            }
        }
    
        public DateTime FileTime
        {
            get
            {
                return this.fileTime;
            }
            set
            {
                this.fileTime = value;
            }
        }
    
        public int Version
        {
            get
            {
                return this.version;
            }
        }
    
        #region Ctor
        public MyFileRWriter(string name, string style)
        {
            this.fileNameExtension = "BTR";
            this.version = 0;
            this.fileTime = DateTime.Now;
            this.fileComment = string.Empty;
            this.authorName = name;
            this.pattern = style;
            this.rawData = null;
        }
    
        public MyFileRWriter(Stream s)
        {
            BinaryReader reader = new BinaryReader(s);
    
            try
            {
                reader = new BinaryReader(s);
                this.fileNameExtension = reader.ReadString();
                if (this.fileNameExtension != "BTR")
                {
                    throw new ArgumentException("格式错误");//文件格式错误
                }
                this.version = reader.ReadInt32();
                if (this.version < 0)
                {
                    throw new ArgumentException("版本错误");//版本错误
                }
                try
                {
                    this.fileTime = DateTime.FromBinary(reader.ReadInt64());
                }
                catch (Exception)
                {
                    throw new ArgumentException("文件时间被损坏");//时间错误
                }
                this.fileComment = reader.ReadString();
                this.authorName = reader.ReadString();
                this.pattern = reader.ReadString();
                this.rawData = reader.ReadBytes((int)(s.Length - s.Position));
            }
            catch (ArgumentException e)
            {
                throw new Exception("文件无法解析:" + e.Message);
            }
            catch (ObjectDisposedException e)
            {
                throw new Exception("文件无法访问:" + e.Message);
            }
            catch (EndOfStreamException e)
            {
                throw new Exception("文件头被损坏:" + e.Message);
            }
            catch (IOException e)
            {
                throw new Exception("文件读取发生致命错误:" + e.Message);
            }
            finally
            {
                reader.Close();
                reader = null;
            }
        }
        #endregion
    
        // Methods
        public void Save(Stream s)
        {
            BinaryWriter writer = new BinaryWriter(s);
            try
            {
                writer.Write(this.fileNameExtension);
                writer.Write(this.version);
                writer.Write(this.fileTime.ToBinary());
                writer.Write(this.fileComment);
                writer.Write(this.authorName);
                writer.Write(this.pattern);
                writer.Write(this.rawData, 0, this.rawData.Length);
            }
            catch (ObjectDisposedException e)
            {
                if (this.exceptionDel != null)
                {
                    this.exceptionDel(e.Source, e.Message);
                }
            }
            catch (EndOfStreamException e)
            {
                if (this.exceptionDel != null)
                {
                    this.exceptionDel(e.Source, e.Message);
                }
            }
            catch (IOException e)
            {
                if (this.exceptionDel != null)
                {
                    this.exceptionDel(e.Source, e.Message);
                }
            }
            finally
            {
                writer.Close();
                writer = null;
            }
        }
    
        #region IDisposable 成员
    
        public void Dispose()
        {
            this.Disposing(true);
        }
    
        protected override void Dispose(bool disposing)
        {
            if (this.rawData != null)
            {
                this.rawData = null;
            }
            GC.Collect();
        }
        #endregion
    }

    2014年8月12日 7:09

答案

  • 你好:

    最好把具体哪行报错,报错的具体错误信息及堆栈信息贴出来看看。

    我的建议是调试一下代码,当到达报错行的时候查看一下内存是否不够用了。读取文件的时候建议按照MSDN文档中的方式来读取:

    http://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx

    //Open the stream and read it back. 
            using (FileStream fs = File.OpenRead(path))
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b,0,b.Length) > 0)
                {
                    Console.WriteLine(temp.GetString(b));
                }
            }
    

    另外就是不太清楚你说的“6X兆字节” 到底是多大是6MB还是6GB还是多少?如果读取大文件的话建议使用内存映射文件来读取,参考MSDN文档及里面的实例代码:

    http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月13日 6:12
    版主

全部回复

  • 会不会是文件本身有问题,造成流读取出错
    2014年8月12日 9:41
  • 会不会是文件流构造的问题(同步流/异步流)?
    2014年8月13日 1:36
  • 你好:

    最好把具体哪行报错,报错的具体错误信息及堆栈信息贴出来看看。

    我的建议是调试一下代码,当到达报错行的时候查看一下内存是否不够用了。读取文件的时候建议按照MSDN文档中的方式来读取:

    http://msdn.microsoft.com/en-us/library/system.io.filestream(v=vs.110).aspx

    //Open the stream and read it back. 
            using (FileStream fs = File.OpenRead(path))
            {
                byte[] b = new byte[1024];
                UTF8Encoding temp = new UTF8Encoding(true);
                while (fs.Read(b,0,b.Length) > 0)
                {
                    Console.WriteLine(temp.GetString(b));
                }
            }
    

    另外就是不太清楚你说的“6X兆字节” 到底是多大是6MB还是6GB还是多少?如果读取大文件的话建议使用内存映射文件来读取,参考MSDN文档及里面的实例代码:

    http://msdn.microsoft.com/en-us/library/system.io.memorymappedfiles.memorymappedfile.aspx


    We are trying to better understand customer views on social support experience, so your participation in this interview project would be greatly appreciated if you have time. Thanks for helping make community forums a great place.
    Click HERE to participate the survey.

    2014年8月13日 6:12
    版主