欢迎您来到GIS动力

加入收藏 免费注册 用户登陆 帮助中心
首页 新闻动态 技术专栏 银杏树下 学习考研 软件下载 求职招聘 许愿瓶 节日祝福 用户中心 精彩推荐 资源搜索 地图
专栏导航: AO开发 | SO开发 | ArcGIS桌面 | 超图桌面 | 开发语言 | 数据库 | WebGIS | 银杏文学 | 研究生考题 | FreeMap 谈天说地
   您现在位于: 首页技术专栏开发语言 → 正文
C#快速随机按行读取大型文本文件
08-01-27 00:00:00 作者:Architie.Weblog 出处:www.architie.com

下面是我实现的一个数据文件随机读取类,可以随机读取大型文本文件的某一行。在我机器上对一个130MB的文本文件,读取第200000的速度从传统做法的400ms提高到了3ms。
一般对文本文件进行读取时,一般采用ReadLine()进行逐行读取。在这种情况下,C#内的FileStream和BufferedStream类处理绰绰有余了。它不会将整个文件全部读入,而是有缓冲的读。但是,要想随机读取某一行,在行数据长度不统一的情况下,如果每次这样遍历到指定行,其效率显然是很低下的。
当然,代价也是有的,引入了第一次打开文件的打开时间,且占用了少部分内存(占用多少是可以设置的,当然占得越小速度也越慢,但最大值也比全部读入要小很多)。

FileConfig.cs:
using System;
using System.Collections.Generic;
using System.Text;

namespace DataBuffer
{
public static class FileConfig
{
public static int STREAM_BUFFER_SIZE = 1024000;
public static int MAP_DISTANCE = 10;
}
}
DataFile.cs:

using System;
using System.Collections.Generic;
using System.Text;
using System.Collections;

namespace DataBuffer
{
public class DataFile
{
///


/// 数据文件名
///
public string fileName = “”;
///
/// 初始化读取完标志
///
public bool done = false;
///
/// 当前流位置
///
public long Position = 0;

private Hashtable head=new Hashtable();
///


/// 文件头部信息
///
public Hashtable Head
{
get
{
return head;
}
set
{
head=value;
}
}

private ArrayList map = new ArrayList();
///


/// 文件地图
///
public ArrayList Map
{
get
{
return map;
}
set
{
map = value;
}
}

private long lines = 0;
///


/// 文件数据行行数
///
public long Lines
{
get
{
return lines;
}
set
{
lines = value;
}
}
}
}
DataBuffer.cs:

using System;
using S

9 7 3 1 2 3 4 5 6 4 8 :

(本文已被浏览 次)
发布人:admin
推荐给好友:发送给好友
上篇新闻:
下篇新闻:
相关评论
发表我的评论
  • 尊重网上道德,遵守《全国人大常委会关于维护互联网安全的决定》及中华人民共和国其他各项有关法律法;
  • 本站有权保留或删除您发表的任何评论内容;
  •   相关文章  
    无相关新闻

    关于我们 友情链接 ┋ 与我在线 ┋ 管理 ┋ TOP
    网站当前版本:GisPower CMS V3.0
    『GIS 动力』- http://www.gispower.org/
    联系我们:webmaster#gispower.org
    Copyright (c) 2003-2007 GisPOwer.Org. All Rights Reserved.

                   滇ICP备05006901号