大文件读取!!!急!!! 大文件读取!!!只想一行行读!!!不想一下子把一个几百M的文件读入!!!!请问有什么方法。网站都快挂了... 解决方案 » 免费领取超大流量手机卡,每月29元包185G流量+100分钟通话, 中国电信官方发货 那就不要用ReadToEnd啦,StreamReader有ReadLine方法,可以读入一行 如果用FileStream 读取FileStream fs = new FileStream(...);byte[] buf = new byte[1024];int count;count=fs.Read(0,buf.Length,buf);配合移动fs.Position可以读取到文件的任何一部分数据 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马上处理掉 但是发现 内存增长很快!!!! 自己些个IHttpModule,使用HttpWorkerRequest来读取。所有的asp.net的大附件上传都是这么做的。有现成的代码可以搜搜 可以一行行读,readline(),也可以从指定位置开始读指定的大小,详细可以看MSDN的帮助,关于文件部分 #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 http://www.cnblogs.com/yuwy/articles/957292.html StreamReader reader = new StreamReader(@"C:\filename.txt");while (!reader.EndOfStream){ //读一行 string strLine = reader.ReadLine();} 发现了 好像是服务器 Response.BinaryWrite 时发生了占用大内存的现象读出来的文件需要下载。。 设置分页用一个循环然后读取StreamReader有ReadLine方法,可以读入一行 我已经分块读了 现在问题是response的时候好像占用大内存这个是怎么解决的呢?多几个人同时访问就受不了了。内存一直要到下载完才释放。 用流来读取,不要全部Load进来 请教思路,保存记录时,如何分别处理“新增”与“保存”这两种情况呢? 关于ListView的问题,谢谢 winform中textbox执行了keyPress事件后 怎么判断txt文件中有多少行,并分别把每行的数据读出来? Windows服务 开启进程不显示窗体,只显示进程 OWC控件问题![急] WinForm的DataGrid中加入CheckBox的方法? 请问我如何知道用户更改了显卡的分辨率? 关于下拉框绑定数据集的小问题--急啊 连续循环发送 SortedList 如何按照value 排序呢
FileStream fs = new FileStream(...);
byte[] buf = new byte[1024];
int count;
count=fs.Read(0,buf.Length,buf);
配合移动fs.Position可以读取到文件的任何一部分数据
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马上处理掉 但是发现 内存增长很快!!!!
所有的asp.net的大附件上传都是这么做的。
有现成的代码可以搜搜
#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
while (!reader.EndOfStream)
{
//读一行
string strLine = reader.ReadLine();
}
读出来的文件需要下载。。
用一个循环
然后读取StreamReader有ReadLine方法,可以读入一行
多几个人同时访问就受不了了。内存一直要到下载完才释放。