一個分割文字檔的小程式 – 回複 “jellyang” 的問題

來源:互聯網
上載者:User
問題來源: http://www.cnblogs.com/del/archive/2010/05/28/1746514.html#1835637

代碼:

unit Unit1;interfaceuses  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  endend

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.