麻豆小视频在线观看_中文黄色一级片_久久久成人精品_成片免费观看视频大全_午夜精品久久久久久久99热浪潮_成人一区二区三区四区

首頁 > 編程 > Delphi > 正文

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

2019-11-18 18:23:42
字體:
供稿:網(wǎng)友


1.AssignFile 過程
 關(guān)聯(lián)一個外部文件名到一個文件變量上。

單元
 System

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

描述
 調(diào)用AssignFile來初始化Delphi代碼中的文件變量。F是一個任何文件類型的文件變量。FileName是一個字符串類型的表達(dá)式,或者,如果擴(kuò)展的語法激活的話,是PChar類型的表達(dá)式。
 調(diào)用AssignFile之后,F(xiàn)就和外部文件關(guān)聯(lián)起來直到F被關(guān)閉。所有在文件變量F上的更多的操作都會操作在名為Filename的外部文件上。
 當(dāng)FileName參數(shù)為空時,AssignFile會將F和標(biāo)準(zhǔn)輸入或標(biāo)準(zhǔn)輸出文件關(guān)聯(lián)起來。如果賦予一個空名字,在調(diào)用了Reset(F)之后,F(xiàn)將引用標(biāo)準(zhǔn)輸入文件,而在調(diào)用了Rewrite(F)之后,F(xiàn)將引用標(biāo)準(zhǔn)輸出文件。
 不要在已經(jīng)打開的文件變量上使用AssignFile。
 注意:為了避免范圍沖突,AssignFile 代替了在早期版本的Delphi產(chǎn)品中可用的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 過程
 改變當(dāng)前目錄

單元
 System

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

描述
 ChDir 會將當(dāng)前目錄改變?yōu)橛蒘或P指定的路徑。如果這個操作失敗,異常EInOutError將會引發(fā)。
 在Windows上,路徑可以包含驅(qū)動器指示符(drive specifier),而這將導(dǎo)致當(dāng)前的驅(qū)動盤同時改變。
 注意:在Delphi中,{$I+} 使用異常來處理運(yùn)行時錯誤。當(dāng)使用{$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 過程
 終止文件變量和外部磁盤文件之間的關(guān)聯(lián)

單元
 System

語法
 procedure CloseFile(var F);

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

 4.CreateDir 函數(shù)
   創(chuàng)建一個新目錄
單元
 SysUtils
語法
 function CreateDir(const Dir: string): Boolean;
描述
 CreateDir 創(chuàng)建一個新目錄。如果新目錄成功創(chuàng)建,則返回值為true,或者如果出現(xiàn)錯誤則返回值為false。
示例
 下面的例子會創(chuàng)建目錄'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 函數(shù)
 從刪除一個磁盤文件
單元
 SysUtils
語法
 function DeleteFile(const FileName: string): Boolean;
描述
 DeleteFile 刪除磁盤上由 FileName 命名的文件。如果文件不能被刪除或者文件不存在,函數(shù)將返回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 函數(shù)
 確定指定的目錄是否存在
單元
 SysUtils
語法
 function DirectoryExists(const Directory: string): Boolean;
描述
 調(diào)用 DirectoryExists 來確定由Directory參數(shù)指定的目錄是否存在。如果目錄存在,函數(shù)返回true。如果目錄不存在,函數(shù)返回false。
 如果輸入的是全稱路徑名(full path name),DirectoryExists 會沿著指定的路徑查找目錄。否則Directory參數(shù)會被認(rèn)為是當(dāng)前目錄的相對路徑。
 FileCtrl 單元(僅用于Windows) 同樣包含一個 DirectoryExists 函數(shù)。然而,F(xiàn)ileCtrl 版本是不贊成的,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 函數(shù)
 返回指定盤符上空閑的字節(jié)數(shù)
單元
 SysUtils
語法
 function DiskFree(Drive: Byte): Int64;
描述
 DiskFree 返回指定驅(qū)動盤()的空閑字節(jié)數(shù),其中 0 = 當(dāng)前盤, 1 = A, 2 = B,等等。如果驅(qū)動盤數(shù)字無效,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 函數(shù)
 返回指定盤符的字節(jié)大小
單元
 SysUtils
語法
 function DiskSize(Drive: Byte): Int64;
描述
 DiskSize 返回指定驅(qū)動盤的字節(jié)大小,其中 0 = 當(dāng)前盤,1 = A, 2 = B, 等等。如果驅(qū)動盤數(shù)字無效,DiskSize返回-1。
 注意:DiskSize 僅在Windows上可用。

9.文件模式常量(File mode constants)
 文件模式常量用于打開和關(guān)閉磁盤文件
單元
 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)
描述  當(dāng)打開和關(guān)閉磁盤文件時,使用文件模式常量。這些常量主要用在這樣的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) 
 文件名稱常量用于以平臺中立的方式表達(dá)文件名稱。
單元
 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;

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

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

12.FileaccessRights 變量 
 當(dāng)應(yīng)用程序被調(diào)用時指向特殊的命令行參數(shù)。
單元
 System
語法
 var FileAccessRights: Integer platform;
描述
 在 Windows 中,F(xiàn)ileAccessRights 變量被忽略。
 在 Linux 中,每個文件都有一組許可位(permission bits)控制著對文件的訪問。當(dāng)創(chuàng)建新文件時,F(xiàn)ileAccessRights 指定了一組默認(rèn)的許可標(biāo)記來使用。當(dāng)你沒有顯式指定要使用的許可位時,F(xiàn)ileCreate 方法使用 FileAccessRights 來設(shè)置它創(chuàng)建的文件的訪問權(quán)力。
13.FileAge 函數(shù) 
 返回文件的OS時間戳(Returns the OS timestamp of a file.)
單元
 SysUtils
語法
 function FileAge(const FileName: string): Integer;
描述
 調(diào)用 FileAge 來獲得由 FileNameto 指定的文件的 OS 時間戳。返回值可以使用 FileDateToDateTime函數(shù)轉(zhuǎn)換為TDateTime對象。如果文件不存在返回值為 -1。
 在Linux中,-1 是一個有效的時間戳。可使用 FileExists 核實文件不存在。
示例
 下面的代碼將一個文件的屬性讀入一組變量中,并在文件屬性對話框中設(shè)置檢查框以表現(xiàn)當(dāng)前的屬性,然后執(zhí)行對話框。如果用戶改變并接受對話框的設(shè)置,代碼將設(shè)置文件屬性以匹配改變的設(shè)置:
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 過程 
 關(guān)閉指定的文件
單元
 SysUtils
語法
 procedure FileClose(Handle: Integer);
描述
 FileClose 關(guān)閉給定文件句柄的文件。句柄在文件使用FileOpen或FileCreate打開時獲得。
 當(dāng)和Delphi語言的文件變量一起使用時,應(yīng)使用 CloseFile 過程代替。
示例
 下面的例子使用了一個按鈕,一個字符串柵格和一個保存對話框在窗體上。單擊按鈕后,用戶將被提示輸入文件名。當(dāng)用戶單OK后,字符串柵格的內(nèi)容將被寫到指定的文件中。附加的信息同樣被寫到文件中,以便文件能夠使用FileRead函數(shù)容易地讀取。
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 函數(shù) 
 創(chuàng)建一個新文件
單元
 SysUtils
語法
 function FileCreate(const FileName: string): Integer; overload;
 function FileCreate(const FileName: string; Rights: Integer): Integer; overload;
描述
 FileCreate 用指定的名稱創(chuàng)建新文件。如果返回值是正數(shù),說明函數(shù)成功而且值是新文件的句柄。返回值是-1說明有錯誤發(fā)生。
 在 Windows中,F(xiàn)ileAccessRights 變量和 Rights 參數(shù)被忽略。


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

下一篇:在Delphi7中實現(xiàn)停靠功能

發(fā)表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發(fā)表
學(xué)習(xí)交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網(wǎng)友關(guān)注

主站蜘蛛池模板: 久久99国产精品免费网站 | 中文字幕在线网站 | 亚洲午夜久久久精品一区二区三区 | 国产成年人小视频 | 黄视频免费在线 | 黄色二区三区 | 日韩精品hd | 日本aaaa片毛片免费观看视频 | av在线免费观看网 | 91超视频| 久久新地址 | 主播粉嫩国产在线精品 | 小雪奶水翁胀公吸小说最新章节 | 国产精品区在线12p 午夜视频色 | 国产69精品久久久久99尤 | 久久视频精品 | 77成人影院 | 本站只有精品 | 欧美亚洲一级 | 深夜小视频在线观看 | 国产1区2区在线 | 欧美福利视频一区二区三区 | av之家在线观看 | 蜜桃免费在线 | 狠狠干五月天 | 国产91九色 | 国产精品视频一区二区噜噜 | 2021国产精品| 久久久久久久久久久久久久av | 又黄又爽免费无遮挡在线观看 | 久久国产精品99久久人人澡 | 一区二区久久精品66国产精品 | 视频一区 日韩 | 欧美大穴| 蜜桃一本色道久久综合亚洲精品冫 | 性少妇chinesevideo | 欧美日韩a∨毛片一区 | 亚洲午夜1000理论片aa | 亚洲一区二区三区精品在线观看 | 一级在线观看 | av在线免费看片 |