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

 

带进度的文件复制器

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

 

 

 

 

 

 代码

读取文件的指定行

1、通过StreamReader的Readline

 

ContractedBlock.gif ExpandedBlockStart.gif 通过StreamReader 读取
            StreamReader sr = new StreamReader("E:\\abc.txt");
            Console.WriteLine(
"Peek读取");
            var
= 0;
            
while (sr.Peek() >= 0)
ExpandedBlockStart.gifContractedBlock.gif            
{
                
if (++== 50000000-1)
ExpandedSubBlockStart.gifContractedSubBlock.gif                
{
                    Console.WriteLine(sr.ReadLine());
                    
break;
                }

                
continue;
            }

            sr.Close();

 

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

Seek()方法的定义如下

public override Seek (
offset,
SeekOrigin origin
)
只要知道offset就可以了!
于是我们可以定义一个类,将每行开始的offset找出来,有了每行开始的offset,读取就自然不成问题了
 
 
ContractedBlock.gif ExpandedBlockStart.gif Code
 public class ReadByLine
ExpandedBlockStart.gifContractedBlock.gif    
{
        
public ReadByLine(string name)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            PositionMap
=new List<long>();
            FileName 
= name;
            Done 
= false;
        }

        
public bool Done
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
get;
            
set;
        }


ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 当前流位置
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public long Position getset; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 文件的行数
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public long Lines getset; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 文件名(包含路径)
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public string FileName getset; }

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
///  行位置列表
        
/// </summary>

ExpandedSubBlockStart.gifContractedSubBlock.gif        public List<long> PositionMap getset; }

        
private StreamReader sr;
        
private FileStream fs;

ExpandedSubBlockStart.gifContractedSubBlock.gif        
/**//// <summary>
        
/// 打开文件
        
/// </summary>
        
/// <returns></returns>

        public bool Open()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//初始化各流
                sr = new StreamReader(FileName);
                InitMap();
                
return true;
            }

            
catch (Exception e)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }

        }


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

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

            Done 
= true;
            sr.Close();
        }


        
public string ReadLine(int line)
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
bool flag = true;
            var str
="";
            
while (flag)
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
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()
ExpandedSubBlockStart.gifContractedSubBlock.gif        
{
            
try
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
//fs.Close();
                sr.Close();
                
return true;
            }

            
catch
ExpandedSubBlockStart.gifContractedSubBlock.gif            
{
                
return false;
            }

        }

    }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值