• 设为首页
  • 点击收藏
  • 手机版
    手机扫一扫访问
    迪恩网络手机版
  • 关注官方公众号
    微信扫一扫关注
    迪恩网络公众号

一个分割文本文件的小程序-回复"jellyang"的问题

原作者: [db:作者] 来自: [db:来源] 收藏 邀请

问题来源: http://www.cnblogs.com/del/archive/2010/05/28/1746514.html#1835637

代码:
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Edit1: TEdit;
    Button1: TButton;
    OpenDialog1: TOpenDialog;
    procedure FormCreate(Sender: TObject);
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.FormCreate(Sender: TObject);
begin
  Edit1.Text := '10'; //10 KB
  Edit1.NumbersOnly := True;
  OpenDialog1.Filter := 'TEXT|*.txt|*.*|*.*';
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  fs: TFileStream;
  ms: TMemoryStream;
  path: string;
  size,sizeEnd,count,i: Integer;
  c: Char;
begin
  if OpenDialog1.Execute then path := OpenDialog1.FileName;
  if not FileExists(path) then Exit;

  fs := TFileStream.Create(path, fmOpenRead);
  fs.Read(c, 1);
  if CharInSet(c, [#$EF, #$FE, #$FF]) then
  begin
    ShowMessage('只适用于 ANSI 格式的文本文件');
    fs.Free;
    Exit;
  end;

  TButton(Sender).Enabled := False;
  size := StrToIntDef(Edit1.Text, 10) * 1024;
  count := fs.Size div size;
  sizeEnd := fs.Size mod size;
  if sizeEnd > 0 then Inc(count);

  ms := TMemoryStream.Create;
  fs.Position := 0;
  for i := 0 to count - 1 do
  begin
    Text := Format('%d/%d', [i+1, count]);
    Application.ProcessMessages;
    if (i = count - 1) then size := sizeEnd;
    ms.Size := size;
    fs.Read(ms.Memory^, size);
    ms.SaveToFile(Format('%s_%.3d.txt', [ChangeFileExt(path, ''), i+1]));
    fs.Position := size * (i+1);
  end;
  fs.Free;
  ms.Free;
  TButton(Sender).Enabled := True;
  Text := '完成';
end;

end.


窗体:
object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 122
  ClientWidth = 231
  Color = clBtnFace
  Font.Charset = DEFAULT_CHARSET
  Font.Color = clWindowText
  Font.Height = -11
  Font.Name = 'Tahoma'
  Font.Style = []
  OldCreateOrder = False
  OnCreate = FormCreate
  PixelsPerInch = 96
  TextHeight = 13
  object Edit1: TEdit
    Left = 80
    Top = 24
    Width = 75
    Height = 21
    TabOrder = 0
    Text = 'Edit1'
  end
  object Button1: TButton
    Left = 80
    Top = 62
    Width = 75
    Height = 25
    Caption = 'Button1'
    TabOrder = 1
    OnClick = Button1Click
  end
  object OpenDialog1: TOpenDialog
    Left = 24
    Top = 40
  end
end


鲜花

握手

雷人

路过

鸡蛋
该文章已有0人参与评论

请发表评论

全部评论

专题导读
上一篇:
微信小程序支付发布时间:2022-07-18
下一篇:
微信小程序中写threejs系列之threejs-miniprogram发布时间:2022-07-18
热门推荐
阅读排行榜

扫描微信二维码

查看手机版网站

随时了解更新最新资讯

139-2527-9053

在线客服(服务时间 9:00~18:00)

在线QQ客服
地址:深圳市南山区西丽大学城创智工业园
电邮:jeky_zhao#qq.com
移动电话:139-2527-9053

Powered by 互联科技 X3.4© 2001-2213 极客世界.|Sitemap