C# system.outmemoryexception异常 文件读取出现异常

byte[]bytedata;if(File.Exists(str_file))bytedata=File.ReadAllBytes(str_file);else{Mes... byte[] bytedata;
if (File.Exists(str_file))
bytedata = File.ReadAllBytes(str_file);
else
{
MessageBox.Show("该文件不存在");
return;
}
try
{
bytedata.Initialize();
string strdata = Encoding.ASCII.GetString(bytedata);
}
catch()
{}
在 bytedata = File.ReadAllBytes(str_file)这句出现system.outmemoryexception的异常,可能是因为str_file这个文件中保存的数据太多了吧,以至于分配内存失败,偶想问下该如何解决这个问题
应该把文件中的数据分几次读到字符串中,还是str_file这个文件分成两个文件保存?
展开
 我来答
niecong55
2011-05-04 · 超过41用户采纳过TA的回答
知道小有建树答主
回答量:94
采纳率:0%
帮助的人:94.9万
展开全部
写一个大文件读取类
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
}
然后,再这么写,我直接用你的东西了
Common.FileSplitSteamReader filestream = new Common.FileSplitSteamReader(str_file,1000);//1000是我自己设定的每次分割的大小,你自己可以随意设定这个值
if (File.Exists(str_file))
bytedata = filestream.SpliteRead();
else
{
return;
}
try
{
bytedata.Initialize();
string strdata = Encoding.ASCII.GetString(bytedata);
}
catch
{ }
追问
按照这种方法分次读出后,不能放到一个字符串里,string strdata = Encoding.ASCII.GetString(bytedata);这一句同样会出现异常;
追答
肯定放不下,把大的分割成小的,是为了存储方便,你要想把他还原成原样,那肯定还是那么大,除了提高机器配置,我是没有好的思路了
百度网友8371ee6
2011-05-04 · TA获得超过2818个赞
知道小有建树答主
回答量:692
采纳率:0%
帮助的人:700万
展开全部
当然是每次读取固定直接长度的文件。否则文件太大肯定内存溢出了。推荐使用流读取文件。代码如下:

Byte[] tmpBuf = new Byte[1024]; //文件内容存放在数组里
FileInfo fi = new FileInfo(fileName);//文件详细信息
Int32 fileSize = (Int32)(fi.Length & 0xFFFFFFFF);//文件的长度。比较用
FileStream fs = new FileStream(fileName, FileMode.Open, FileAccess.Read);//文件流
BinaryReader br = new BinaryReader(fs);//二进制流。使用它读取文件内容
rLen = br.Read(tmpBuf, 0, readlength);//rLen 是实际读取的长度。这句代码,将文件fileName的内容读取readlength长度到数组tmpBuf 里。然后流会抛弃度过的内容。下次读取的时候会自动从readlength+1开始读取。readlength长度自己定义。可以定义为1024

分太少,懒得写了。
已赞过 已踩过<
你对这个回答的评价是?
评论 收起
推荐律师服务: 若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询

为你推荐:

下载百度知道APP,抢鲜体验
使用百度知道APP,立即抢鲜体验。你的手机镜头里或许有别人想知道的答案。
扫描二维码下载
×

类别

我们会通过消息、邮箱等方式尽快将举报结果通知您。

说明

0/200

提交
取消

辅 助

模 式