加载中…
个人资料
  • 博客等级:
  • 博客积分:
  • 博客访问:
  • 关注人气:
  • 获赠金笔:0支
  • 赠出金笔:0支
  • 荣誉徽章:
正文 字体大小:

Delphi中的读文件操作(读CSV文件到StringGrid中)

(2010-05-02 08:59:09)
标签:

delphi

文件操作

杂谈

分类: DELPHI

  一、无类型文件,使用BlockRead读入文件

 

const BlockSize=40960;

......

 

procedure TCsvOperate.OpenFileExecute(Sender: TObject);
var
  f: file;
  NumRead, i, pt: integer;
  Buf: array[1..BlockSize] of char;
  str: string;
  T1, T2: Int64;
begin
  if OpenDialog1.Execute then
  begin

    sg0.RowCount:=2;
    sg0.ColCount:=1;

    AssignFile(f, OpenDialog1.FileName);
    Reset(f, 1);

    LnCnt_T0 := 0;
    i := 0;
    T1 := GetTickCount;
    str := '';
    sg0.Visible:=False;
    repeat
      BlockRead(f, Buf, SizeOf(Buf), NumRead);
      if NumRead > 0 then
      begin
        pt := 1;
        while pt <= NumRead do
        begin
          if (Buf[pt] = #$A) then
          begin
            SendMessage(self.Handle, LineStrOK, Integer(@str), 0);
            inc(i);
            str := '';
          end else str := str + Buf[pt];
          inc(pt);
          Statusbar1.Panels[0].Text := IntToStr(i);
        end;
      end else if str <> '' then
        SendMessage(self.Handle, LineStrOK, Integer(@str), 0);
      Application.ProcessMessages;
    until NumRead = 0;
    CloseFile(f);
    sg0.RowCount := i;
    sg0.Visible:=true;

    T2 := GetTickCount;
    ShowMessage('程序共运行时间(秒):' + IntToStr((T2 - T1) div 1000));
  end;

end;

......

procedure TCsvOperate.ProcessLineStr(var msg: TMessage);
var
  LnStr: string;
  LineT0: TStringList;
begin
  LnStr := string(Pointer(Msg.wParam)^);

  LineT0 := TStringList.Create;
  CnCnt_T0 := SplitStrings(Pchar(LnStr), LineT0);
  //CnCnt_T0 := ExtractStrings([','],[],Pchar(LnStr), LineT0);
  if sg0.ColCount < CnCnt_T0 then sg0.ColCount := CnCnt_T0;
  if sg0.RowCount < LnCnt_T0 then sg0.RowCount := sg0.RowCount + 1000;
  sg0.Rows[LnCnt_T0].Assign(LineT0);
  Inc(LnCnt_T0);
end;

 

   二、使用TFileStream文件流操作

procedure TCsvOperate.ToolButton6Click(Sender: TObject);
var
  fs:TFileStream;
  Buf: array[1..BlockSize] of char;
  NumRead, i, pt:integer;
  str: string;
  T1, T2, fileSize,ptSize: Int64;
begin
  if OpenDialog1.Execute then
  begin
    sg0.RowCount:=2;
    sg0.ColCount:=1;
    fs:=TFileStream.Create(OpenDialog1.FileName,fmOpenRead);
    fileSize:=fs.Size;
    ptSize:=0;  //已读入的字节数
    ProgressBar1.Visible:=True;
    try
      LnCnt_T0 := 0;
      i := 0;
      T1 := GetTickCount;
      str := '';
      sg0.Visible:=false;
      repeat
        NumRead:=fs.Read(Buf,BlockSize);
        if NumRead > 0 then
        begin
          pt := 1;
          while pt <= NumRead do
          begin
            if (Buf[pt] = #$A) then
            begin
              SendMessage(self.Handle, LineStrOK, Integer(@str), 0);
              inc(i);
              str := '';
            end else str := str + Buf[pt];
            inc(pt);

          end;
        end else if str <> '' then
          SendMessage(self.Handle, LineStrOK, Integer(@str), 0);
        Application.ProcessMessages;
        ptSize:=ptSize+NumRead;
        ProgressBar1.Position:= (ptSize*100) div fileSize;
      until NumRead = 0;
      sg0.RowCount := i;
      sg0.Visible:=true;

      T2 := GetTickCount;
      Statusbar1.Panels[0].Text := '行数:'+IntToStr(i);
      StatusBar1.Panels[1].Text:='耗时:' + IntToStr((T2 - T1) div 1000)+'秒';
      ProgressBar1.Visible:=False;
    finally
      fs.Free;
    end;
  end;
end;

 

  三、两种方法在BlockSize大小一样时,速度一致。

0

阅读 收藏 喜欢 打印举报/Report
  

新浪BLOG意见反馈留言板 欢迎批评指正

新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 产品答疑

新浪公司 版权所有