前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >c# IO操作(带进度的文件复制器,读取文本文件的指定行)

c# IO操作(带进度的文件复制器,读取文本文件的指定行)

作者头像
JadePeng
发布2018-01-18 09:45:26
1.6K0
发布2018-01-18 09:45:26
举报

带进度的文件复制器

     基本原理就是通过Stream的BeginRead来异步复制文件,同时刷新进度条的状态

代码

读取文件的指定行

1、通过StreamReader的Readline

通过StreamReader 读取

StreamReader sr = new StreamReader("E:\\abc.txt");
Console.WriteLine("Peek读取");
var i = 0;
while (sr.Peek() >= 0)
{
 if (++i == 50000000-1)
{
 Console.WriteLine(sr.ReadLine());
 break;
}
 continue;
}
sr.Close();

2、通过 FileStream.seek()来读取

Seek()方法的定义如下

public override long Seek (
long offset,
SeekOrigin origin
)

只要知道offset就可以了!

于是我们可以定义一个类,将每行开始的offset找出来,有了每行开始的offset,读取就自然不成问题了

Code

 public class ReadByLine
    {
        public ReadByLine(string name)
        {
            PositionMap=new List<long>();
            FileName = name;
            Done = false;
        }
        public bool Done
        {
            get;
            set;
        }

        /**//// <summary>
        /// 当前流位置
        /// </summary>
        public long Position { get; set; }

        /**//// <summary>
        /// 文件的行数
        /// </summary>
        public long Lines { get; set; }

        /**//// <summary>
        /// 文件名(包含路径)
        /// </summary>
        public string FileName { get; set; }

        /**//// <summary>
        ///  行位置列表
        /// </summary>
        public List<long> PositionMap { get; set; }

        private StreamReader sr;
        private FileStream fs;

        /**//// <summary>
        /// 打开文件
        /// </summary>
        /// <returns></returns>
        public bool Open()
        {
            try
            {
                //初始化各流
                sr = new StreamReader(FileName);
                InitMap();
                return true;
            }
            catch (Exception e)
            {
                return false;
            }
        }

        private void InitMap()
        {
      
            Lines = 1;
            Position = 0;
            //在地图中加入首条数据的位置信息
            PositionMap.Add(Position);

            //顺序建立文件地图
            while (sr.Peek()>0)
            {
                var str = sr.ReadLine();
                Position +=Encoding.UTF8.GetBytes(str).Length+ 2;
                PositionMap.Add(Position);
                Lines++;
            }
            Done = true;
            sr.Close();
        }

        public string ReadLine(int line)
        {
            bool flag = true;
            var str="";
            while (flag)
            {
                if (!Done) continue;
                fs = new FileStream(FileName, FileMode.Open);
                fs.Seek(PositionMap[line], SeekOrigin.Begin);
                var reader = new StreamReader(fs);
                str = reader.ReadLine();
                reader.Close();
                fs.Close();
                flag = false;
            }
            return str;
        }

        public bool Close()
        {
            try
            {
                //fs.Close();
                sr.Close();
                return true;
            }
            catch
            {
                return false;
            }
        }
    }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2008-09-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
目录
  • 带进度的文件复制器
  • 读取文件的指定行
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档