国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > Delphi > 正文

Delphi例程-文件管理例程(1~15)

2019-11-18 18:23:42
字體:
來源:轉載
供稿:網友


1.AssignFile 過程
 關聯一個外部文件名到一個文件變量上。

單元
 System

語法
 PRocedure AssignFile(var F; FileName: string);

描述
 調用AssignFile來初始化Delphi代碼中的文件變量。F是一個任何文件類型的文件變量。FileName是一個字符串類型的表達式,或者,如果擴展的語法激活的話,是PChar類型的表達式。
 調用AssignFile之后,F就和外部文件關聯起來直到F被關閉。所有在文件變量F上的更多的操作都會操作在名為Filename的外部文件上。
 當FileName參數為空時,AssignFile會將F和標準輸入或標準輸出文件關聯起來。如果賦予一個空名字,在調用了Reset(F)之后,F將引用標準輸入文件,而在調用了Rewrite(F)之后,F將引用標準輸出文件。
 不要在已經打開的文件變量上使用AssignFile。
 注意:為了避免范圍沖突,AssignFile 代替了在早期版本的Delphi產品中可用的Assign過程。然而為了向后兼容Assign仍然是可用的。
示例
var
  F: TextFile;
  S: string;
begin
  if OpenDialog1.Execute then            { Display Open dialog box }
  begin
    AssignFile(F, OpenDialog1.FileName); { File selected in dialog }
    Reset(F);
    Readln(F, S);                        { Read first line of file }
    Edit1.Text := S;                     { Put string in a TEdit control }
    CloseFile(F);
  end;
end;

2.ChDir 過程
 改變當前目錄

單元
 System

語法
 procedure ChDir(const S: string); overload;
 procedure ChDir(P: PChar); overload;

描述
 ChDir 會將當前目錄改變為由S或P指定的路徑。如果這個操作失敗,異常EInOutError將會引發。
 在Windows上,路徑可以包含驅動器指示符(drive specifier),而這將導致當前的驅動盤同時改變。
 注意:在Delphi中,{$I+} 使用異常來處理運行時錯誤。當使用{$I-}時,要使用IOResult來檢查I/O錯誤。
示例
begin
  {$I-}
  { Change to directory specified in Edit1 }
  ChDir(Edit1.Text);
  if IOResult <> 0 then
    MessageDlg('Cannot find directory', mtWarning, [mbOk], 0);
end;

3.CloseFile 過程
 終止文件變量和外部磁盤文件之間的關聯

單元
 System

語法
 procedure CloseFile(var F);

描述
 由于命名沖突,CloseFile代替了Close過程。使用CloseFile過程而不是Close來終止文件變量和外部磁盤文件之間的關聯。
 F是一個使用Reset,Rewrite或Append打開的任何文件類型的文件變量。和F關聯的外部文件會完全地更新然后關閉并釋放文件句柄便于重用。
 注意:{$I+} 使用異常來處理運行時錯誤。當使用{$I-}時,要使用IOResult檢查I/O 錯誤。

 4.CreateDir 函數
   創建一個新目錄
單元
 SysUtils
語法
 function CreateDir(const Dir: string): Boolean;
描述
 CreateDir 創建一個新目錄。如果新目錄成功創建,則返回值為true,或者如果出現錯誤則返回值為false。
示例
 下面的例子會創建目錄'C:/temp',如果目錄不存在的話。

uses FileCtrl;

procedure TForm1.Button1Click(Sender: TObject);
begin
  if not DirectoryExists('c:/temp') then
    if not CreateDir('C:/temp') then
    raise Exception.Create('Cannot create c:/temp');
end;

5.DeleteFile 函數
 從刪除一個磁盤文件
單元
 SysUtils
語法
 function DeleteFile(const FileName: string): Boolean;
描述
 DeleteFile 刪除磁盤上由 FileName 命名的文件。如果文件不能被刪除或者文件不存在,函數將返回false。
示例
 
if FileExists(FileName) then
  if MessageDlg('Do you really want to delete ' + ExtractFileName(FileName) + '?'), mtConfirmation, [mbYes, mbNo], 0, mbNo) = IDYes then
    DeleteFile(FileName);

6.DirectoryExists 函數
 確定指定的目錄是否存在
單元
 SysUtils
語法
 function DirectoryExists(const Directory: string): Boolean;
描述
 調用 DirectoryExists 來確定由Directory參數指定的目錄是否存在。如果目錄存在,函數返回true。如果目錄不存在,函數返回false。
 如果輸入的是全稱路徑名(full path name),DirectoryExists 會沿著指定的路徑查找目錄。否則Directory參數會被認為是當前目錄的相對路徑。
 FileCtrl 單元(僅用于Windows) 同樣包含一個 DirectoryExists 函數。然而,FileCtrl 版本是不贊成的,SysUtils 版本是首選的,即使代碼不需要跨平臺(However, the FileCtrl version is deprecated, and the SysUtils version preferred, even if the code does not need to be cross-platform)。

7.DiskFree 函數
 返回指定盤符上空閑的字節數
單元
 SysUtils
語法
 function DiskFree(Drive: Byte): Int64;
描述
 DiskFree 返回指定驅動盤()的空閑字節數,其中 0 = 當前盤, 1 = A, 2 = B,等等。如果驅動盤數字無效,DiskFree 返回-1。
 注意:DiskFree 僅在Windows上可用。
示例
var
  S: string;
  AmtFree: Int64;
  Total:   Int64;
begin
  AmtFree := DiskFree(0);
  Total := DiskSize(0);
  S := IntToStr(AmtFree div Total) + 'percent of the space on drive 0 is free: ' (AmtFree div 1024) + ' Kbytes free. ';
  Label1.Caption := S;
end;

8.DiskSize 函數
 返回指定盤符的字節大小
單元
 SysUtils
語法
 function DiskSize(Drive: Byte): Int64;
描述
 DiskSize 返回指定驅動盤的字節大小,其中 0 = 當前盤,1 = A, 2 = B, 等等。如果驅動盤數字無效,DiskSize返回-1。
 注意:DiskSize 僅在Windows上可用。

9.文件模式常量(File mode constants)
 文件模式常量用于打開和關閉磁盤文件
單元
 System
語法
 const fmClosed = $D7B0;  // closed file
 const fmInput  = $D7B1;  // reset file (TTextRec)
 const fmOutput = $D7B2;  // rewritten file (TTextRec)
 const fmInOut  = $D7B3;  // reset or rewritten file (TFileRec)
 const fmCRLF   = $8      // DOS-style EoL and EoF markers (TTextRec)
 const fmMask   = $D7B3;  // mask out fmCRLF flag (TTextRec)
描述  當打開和關閉磁盤文件時,使用文件模式常量。這些常量主要用在這樣的Delphi代碼中,TFileRec和TTextRec的Mode字段包含這些值中的某個值(These constants are used primarily in Delphi code, where the Mode field of TFileRec and TTextRec contain one of these values.)。

10.文件名稱常量(File name constants) 
 文件名稱常量用于以平臺中立的方式表達文件名稱。
單元
 SysUtils
語法
  const
 PathDelim  = {$IFDEF MSWINDOWS} '/'; {$ELSE} '/'; {$ENDIF}
 DriveDelim = {$IFDEF MSWINDOWS} ':'; {$ELSE} '';  {$ENDIF}
 PathSep    = {$IFDEF MSWINDOWS} ';'; {$ELSE} ':'; {$ENDIF}
描述
 文件名稱常量指定了在Windows和linux中不同的的定界符和分隔符(delimiter and separator)。

11.文件打開模式常量(File open mode constants) 
 打開打開模式常量用于控制對文件或流的訪問模式。
單元
 SysUtils
語法
On Windows:
  const
    fmCreate         = $FFFF;
    fmOpenRead       = $0000;
    fmOpenWrite      = $0001;
    fmOpenReadWrite  = $0002;

    fmShareCompat    = $0000 platform;
    fmShareExclusive = $0010;
    fmShareDenyWrite = $0020;
    fmShareDenyRead  = $0030 platform;
    fmShareDenyNone  = $0040;

On Linux:
  const
    fmOpenRead       = O_RDONLY;
    fmOpenWrite      = O_WRONLY;
    fmOpenReadWrite  = O_RDWR;
    fmShareExclusive = $0010;
    fmShareDenyWrite = $0020;
    fmShareDenyNone  = $0030;

描述
 當文件或流被打開時,文件打開模式常量用于控制文件或流能夠如何共享。
 TFileStream構造函數有一個Mode參數,你能夠設置為這些常量中的一個:
Constant  Definition

fmCreate  如果文件存在,那么將打開用于寫訪問,否則會創建新文件。其他的常量都聲明在 SysUtils 單元,而這個常量聲明在 Classes 單元中
fmOpenRead  僅以讀訪問方式打開
fmOpenWrite  僅以寫訪問方式打開
fmOpenReadWrite 以讀寫訪問方式打開
fmShareCompat 和FCB打開的方法兼容。不要在跨平臺應用程序中使用這個模式
fmShareExclusive 讀寫訪問被拒絕
fmShareDenyWrite 寫訪問被拒絕
fmShareDenyRead  讀訪問被拒絕。不要在跨平臺應用程序中使用這個模式
fmShareDenyNone 允許其他代碼進行完全的訪問

12.FileaccessRights 變量 
 當應用程序被調用時指向特殊的命令行參數。
單元
 System
語法
 var FileAccessRights: Integer platform;
描述
 在 Windows 中,FileAccessRights 變量被忽略。
 在 Linux 中,每個文件都有一組許可位(permission bits)控制著對文件的訪問。當創建新文件時,FileAccessRights 指定了一組默認的許可標記來使用。當你沒有顯式指定要使用的許可位時,FileCreate 方法使用 FileAccessRights 來設置它創建的文件的訪問權力。
13.FileAge 函數 
 返回文件的OS時間戳(Returns the OS timestamp of a file.)
單元
 SysUtils
語法
 function FileAge(const FileName: string): Integer;
描述
 調用 FileAge 來獲得由 FileNameto 指定的文件的 OS 時間戳。返回值可以使用 FileDateToDateTime函數轉換為TDateTime對象。如果文件不存在返回值為 -1。
 在Linux中,-1 是一個有效的時間戳。可使用 FileExists 核實文件不存在。
示例
 下面的代碼將一個文件的屬性讀入一組變量中,并在文件屬性對話框中設置檢查框以表現當前的屬性,然后執行對話框。如果用戶改變并接受對話框的設置,代碼將設置文件屬性以匹配改變的設置:
procedure TFMForm.Properties1Click(Sender: TObject);
var
  Attributes, NewAttributes: Word;
begin
  with FileAttrForm do
  begin
    FileDirName.Caption := FileList.Items[FileList.ItemIndex];
    { set box caption }
    PathName.Caption := FileList.Directory;
    { show directory name }
    ChangeDate.Caption :=
      DateTimeToStr(FileDateToDateTime(FileAge(FileList.FileName)));
    Attributes := FileGetAttr(FileDirName.Caption);
    { read file attributes }
    ReadOnly.Checked := (Attributes and SysUtils.faReadOnly) = faReadOnly;
    Archive.Checked := (Attributes and faArchive) = faArchive;
    System.Checked := (Attributes and faSysFile) = faSysFile;
    Hidden.Checked := (Attributes and faHidden) = faHidden;
    if ShowModal <> id_Cancel then { execute dialog box }
    begin
      NewAttributes := Attributes;
      { start with original attributes }
      if ReadOnly.Checked then
        NewAttributes := NewAttributes or SysUtils.faReadOnly
      else
        NewAttributes := NewAttributes and not SysUtils.faReadOnly;
      if Archive.Checked then
        NewAttributes := NewAttributes or faArchive
      else
        NewAttributes := NewAttributes and not faArchive;
      if System.Checked then
        NewAttributes := NewAttributes or faSysFile
      else
        NewAttributes := NewAttributes and not faSysFile;
      if Hidden.Checked then
        NewAttributes := NewAttributes or faHidden
      else
        NewAttributes := NewAttributes and not faHidden;
      if NewAttributes <> Attributes then { if anything changed... }
        FileSetAttr(FileDirName.Caption, NewAttributes);
         { ...write the new values }
    end;
  end;
end;
 
14.FileClose 過程 
 關閉指定的文件
單元
 SysUtils
語法
 procedure FileClose(Handle: Integer);
描述
 FileClose 關閉給定文件句柄的文件。句柄在文件使用FileOpen或FileCreate打開時獲得。
 當和Delphi語言的文件變量一起使用時,應使用 CloseFile 過程代替。
示例
 下面的例子使用了一個按鈕,一個字符串柵格和一個保存對話框在窗體上。單擊按鈕后,用戶將被提示輸入文件名。當用戶單OK后,字符串柵格的內容將被寫到指定的文件中。附加的信息同樣被寫到文件中,以便文件能夠使用FileRead函數容易地讀取。
procedure TForm1.Button1Click(Sender: TObject);
var
  BackupName: string;
  FileHandle: Integer;
  StringLen: Integer;
  X: Integer;
  Y: Integer;
begin
  if SaveDialog1.Execute then
  begin
    if FileExists(SaveDialog1.FileName) then
    begin
      BackupName := ExtractFileName(SaveDialog1.FileName);
      BackupName := ChangeFileExt(BackupName, '.BAK');
      if not RenameFile(SaveDialog1.FileName, BackupName) then
        raise Exception.Create('Unable to create backup file.');
    end;
    FileHandle := FileCreate(SaveDialog1.FileName);
    { Write out the number of rows and columns in the grid. }
    FileWrite(FileHandle,
      StringGrid1.ColCount, SizeOf(StringGrid1.ColCount));
    FileWrite(FileHandle,
      StringGrid1.RowCount, SizeOf(StringGrid1.RowCount));
    for X := 0 to StringGrid1.ColCount - 1 do
    begin
      for Y := 0 to StringGrid1.RowCount - 1 do
      begin
        { Write out the length of each string, followed by the string itself. }
        StringLen := Length(StringGrid1.Cells[X,Y]);
        FileWrite(FileHandle, StringLen, SizeOf(StringLen));
        FileWrite(FileHandle,
          StringGrid1.Cells[X,Y], Length(StringGrid1.Cells[X,Y]);
      end;
    end;
    FileClose(FileHandle);
  end;
end;

15.FileCreate 函數 
 創建一個新文件
單元
 SysUtils
語法
 function FileCreate(const FileName: string): Integer; overload;
 function FileCreate(const FileName: string; Rights: Integer): Integer; overload;
描述
 FileCreate 用指定的名稱創建新文件。如果返回值是正數,說明函數成功而且值是新文件的句柄。返回值是-1說明有錯誤發生。
 在 Windows中,FileAccessRights 變量和 Rights 參數被忽略。


上一篇:用Delphi建立通訊與數據交換服務器—Transceiver技術剖析(下)

下一篇:在Delphi7中實現停靠功能

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 双流县| 金昌市| 内丘县| 芜湖县| 柳州市| 湘阴县| 朔州市| 谷城县| 洛川县| 侯马市| 天台县| 仙游县| 琼海市| 泾川县| 措美县| 施甸县| 崇左市| 庆元县| 达州市| 深泽县| 尼玛县| 江西省| 张家口市| 新丰县| 房产| 洮南市| 封丘县| 瓮安县| 孟村| 喀喇| 新竹市| 班玛县| 垦利县| 界首市| 克什克腾旗| 杭锦后旗| 隆德县| 抚顺市| 哈尔滨市| 双流县| 社旗县|