大文件读取!!!
只想一行行读!!!不想一下子把一个几百M的文件读入!!!!
请问有什么方法。网站都快挂了...

解决方案 »

  1.   

    那就不要用ReadToEnd啦,StreamReader有ReadLine方法,可以读入一行
      

  2.   

    如果用FileStream 读取
    FileStream fs = new FileStream(...);
    byte[] buf = new byte[1024];
    int count;
    count=fs.Read(0,buf.Length,buf);
    配合移动fs.Position可以读取到文件的任何一部分数据
      

  3.   

    sFile = new FileStream(Server.MapPath(flvName), FileMode.Open, FileAccess.Read);
                            sFile.Seek(seekat, SeekOrigin.Begin);
                            int offset = 0;
                            int incnum = sFile.Read(byData, offset, byData.Length);
                            while (incnum > 0)
                            {
                                offset += incnum;
                                incnum = sFile.Read(byData, 0, byData.Length);
                            }
    我这样读取 byData马上处理掉 但是发现 内存增长很快!!!!
      

  4.   

    自己些个IHttpModule,使用HttpWorkerRequest来读取。
    所有的asp.net的大附件上传都是这么做的。
    有现成的代码可以搜搜
      

  5.   

    可以一行行读,readline(),也可以从指定位置开始读指定的大小,详细可以看MSDN的帮助,关于文件部分
      

  6.   


    #region //大文件读取类
        public class FileSplitSteamReader : FileStream
        {
            #region //文件分块读
            /// <summary>
            /// 用于普通文件读取
            /// </summary>
            /// <param name="sourceFileName">文件的路径</param>
            public FileSplitSteamReader(string sourceFileName)
                : base(sourceFileName, FileMode.Open, FileAccess.Read)
            {
                this.sourceFileName = sourceFileName;
            }
            /// <summary>
            /// 用于大文件读取
            /// </summary>
            /// <param name="sourceFileName">文件的路径</param>
            /// <param name="splitSize">切分大小</param>
            public FileSplitSteamReader(string sourceFileName, int splitSize)
                : base(sourceFileName, FileMode.Open, FileAccess.Read)
            {
                this.sourceFileName = sourceFileName;
                this.splitSize = splitSize;
            }        private string sourceFileName;
            /// <summary>
            /// 获取文件的路径
            /// </summary>
            public string SourceFileName
            {
                get { return sourceFileName; }
            }        private long splitSize;
            /// <summary>
            /// 每次切分文件大小splitSize
            /// </summary>
            public long SplitSize
            {
                get { return splitSize; }
            }        /// <summary>
            /// 文件的大小
            /// </summary>
            public long FileSize
            {
                get { return this.Length; }
            }        private long readTimes = 1;
            /// <summary>
            /// 当前读取次数块号
            /// </summary>
            public long ReadTimes
            {
                get { return readTimes; }
            }        private bool judge = false;
            /// <summary>
            /// 用于判断是否执行到最后一块.读完为ture,未读完为false.
            /// </summary>
            public bool Judge
            {
                get { return judge; }
                set { judge = value; }
            }        /// <summary>
            /// 最后一次读取文件的大小
            /// </summary>
            public long FinilReadSize
            {
                get
                {
                    if (splitSize == 0)
                        return 1024 * 1024 * 3;
                    else
                        return this.FileSize - (this.FileSize / (long)this.splitSize) * (long)this.splitSize;            }
            }
            public int CurrentReadSize;
            /// <summary>
            /// 开始读取文件
            /// </summary>
            /// <returns>以Bitmap类型返回每次读取文件的内容</returns>
            public byte[] SpliteRead()
            {
                FileBlockSteamReaderEventArgs Fbsr = new FileBlockSteamReaderEventArgs();
                byte[] timeReadContect;
                this.Seek(splitSize * (readTimes - 1), SeekOrigin.Begin);
                if (readTimes < (this.FileSize / this.splitSize + 1))
                {
                    Fbsr.ReadPercent = (int)(((float)this.Position / this.FileSize) * 100);
                    timeReadContect = new byte[this.splitSize];
                }
                else
                {
                    timeReadContect = new byte[this.FinilReadSize];
                    judge = true;
                    Fbsr.ReadPercent = 100;
                }
                CurrentReadSize = timeReadContect.Length;
                this.Read(timeReadContect, 0, timeReadContect.Length);
                FileABlockReadEndEvent(Fbsr);
                readTimes++;
                return timeReadContect;
            }
            public event EventHandler<FileBlockSteamReaderEventArgs> ABlockReadEndEvent;
            public event EventHandler FinishAllReadEvent;        /// <summary>
            /// 文件读取完当前块当前事件
            /// </summary>
            /// <param name="e"></param>
            public void FileABlockReadEndEvent(FileBlockSteamReaderEventArgs e)
            {
                if (ABlockReadEndEvent != null)
                {
                    this.ABlockReadEndEvent(this, e);
                }
            }
            /// <summary>
            /// 文件读取完最后一块的事件
            /// </summary>
            public void FileFinishAllReadEvent()
            {
                if (FinishAllReadEvent != null)
                {
                    this.FinishAllReadEvent(this, new EventArgs());
                }
            }
            #endregion
        }
        public class FileBlockSteamReaderEventArgs : EventArgs
        {
            private int readPercent;        public int ReadPercent
            {
                get { return readPercent; }
                set { readPercent = value; }
            }
        } 
        #endregion
      

  7.   

    http://www.cnblogs.com/yuwy/articles/957292.html
      

  8.   

    StreamReader reader = new StreamReader(@"C:\filename.txt");
    while (!reader.EndOfStream)
    {
        //读一行
        string strLine = reader.ReadLine();
    }
      

  9.   

    发现了 好像是服务器 Response.BinaryWrite 时发生了占用大内存的现象
    读出来的文件需要下载。。
      

  10.   

    设置分页
    用一个循环
    然后读取StreamReader有ReadLine方法,可以读入一行 
      

  11.   

    我已经分块读了 现在问题是response的时候好像占用大内存这个是怎么解决的呢?
    多几个人同时访问就受不了了。内存一直要到下载完才释放。
      

  12.   

    用流来读取,不要全部Load进来