1. 文件管理類函數(shù) 判斷文件是否存在 FileExists 判斷文件夾是否存在 DirectoryExists 刪除文件 DeleteFile; Windows.DeleteFile 刪除文件夾 RemoveDir; RemoveDirectory 獲取當(dāng)前文件夾 GetCurrentDir 設(shè)置當(dāng)前文件夾 SetCurrentDir; ChDir; SetCurrentDirectory 獲取指定驅(qū)動器的當(dāng)前路徑名 GetDir 文件改名 RenameFile 建立文件夾 CreateDir; CreateDirectory; ForceDirectories 刪除空文件夾 RemoveDir; RemoveDirectory 獲取當(dāng)前文件的版本號 GetFileVersion 獲取磁盤空間 DiskSize; DiskFree 查找一個文件 FileSearch 搜索文件 FindFirst; FindNext; FindClose 讀取與設(shè)置文件屬性 FileGetAttr; FileSetAttr 獲取文件的創(chuàng)建時間 FileAge; FileDateToDateTime
1.1 API 文件處理函數(shù) 1. GetWindowsDirectory - 獲取 Windows 所在目錄 //聲明:GetWindowsDirectory( lpBuffer: PChar;{緩沖區(qū)} uSize: UINT {緩沖區(qū)大小}): num := GetWindowsDirectory(arr, MAX_PATH); 2. GetSystemDirectory - 返回 System 文件夾路徑 //聲明: num := GetSystemDirectory(arr, MAX_PATH); 3.GetTempPath - 獲取臨時文件夾路徑 //聲明:
4.GetTempFileName - 生成一個臨時文件名
5.CopyFile - 復(fù)制文件 //聲明: 6.CreateDirectory - 建立文件夾 CreateDirectory(PChar(dir),
7.CreateDirectoryEx - 根據(jù)模版建立文件夾 CreateDirectoryEx(PChar(TDir), PChar(Dir),nil);
8.RemoveDirectory - 刪除空目錄//聲明: if RemoveDirectory(PChar(Dir))then
9.SetCurrentDirectory、GetCurrentDirectory - 設(shè)置與獲取當(dāng)前目錄 SetCurrentDirectory('c:\temp'); GetCurrentDirectory(SizeOf(buf), buf);
10.SetVolumeLabel - 設(shè)置磁盤卷標(biāo) SetVolumeLabel('c:\', 'NewLabel');
2. INI 文件讀取 private List: TStrings; Fini: TIniFile; Path: string; Section,Key: string; { Private declarations } public { Public declarations } end; var MainForm: TMainForm; implementation uses ShellAPI; {$R *.dfm} { INI File =============================================================== } procedure TMainForm.FormCreate(Sender: TObject); begin Path := ChangeFileExt(ParamStr(0),'.ini'); Fini := TIniFile.Create(Path); end; //寫入 Write INI procedure TMainForm.Button1Click(Sender: TObject); begin Section := 'Delphi xe Title'; Key := 'Astring Key'; Fini.WriteString(Section,Key,'a_string value'); ShellExecute(0,'open',PAnsiChar(Path),nil,nil,sw_show); end; //讀取 Read INI procedure TMainForm.Button2Click(Sender: TObject); var s: string; begin s := Fini.ReadString('Delphi xe Title','Astring Key',''); ShowMessage(s); end; //讀取 字節(jié)Read ReadSections ....Section procedure TMainForm.Button3Click(Sender: TObject); begin List := TStringList.Create; Fini.ReadSections(List); ShowMessage(List.Text); //Delphi xe Title end; //讀取關(guān)鍵字 Read ReadSection ...Key procedure TMainForm.Button4Click(Sender: TObject); begin List := TStringList.Create; Fini.ReadSection('Delphi xe Title',List); ShowMessage(List.Text); //AString Key end; //刪除節(jié)點DeleteKey procedure TMainForm.Button5Click(Sender: TObject); begin Fini.DeleteKey('Delphi xe Title','AString Key'); end; //刪除全部 EraseSection procedure TMainForm.Button6Click(Sender: TObject); begin Fini.EraseSection('Delphi xe Title'); end; //Other... procedure TMainForm.Button7Click(Sender: TObject); var b: Boolean; s: string; begin b := Fini.SectionExists('Delphi xe Title'); {讀取 小節(jié)} ShowMessage(BoolToStr(b)); b := Fini.ValueExists('Delphi xe Title','AString Key'); {讀取} ShowMessage(BoolToStr(b)); s := Fini.FileName; {文件名} ShowMessage(s); end;
3.文本文件 讀寫 Delphi 支持三種文件類型: 文本文件、記錄文件、無類型文件。 var F: Text; FileName: string = 'D:\Delphi Study N\15.Delphi File\a.txt'; { Text File ================================================ } //寫入 Write Txt procedure TMainForm.Button8Click(Sender: TObject); begin AssignFile(F,FileName); Rewrite(F); //沒有就覆蓋 writeln(F,'One'); Writeln(F,'Two'); CloseFile(F); ShellExecute(0,'Open',PAnsiChar(FileName),nil,nil,SW_SHOW); end; //讀取 Read Txt procedure TMainForm.Button9Click(Sender: TObject); var s: string; begin AssignFile(F,FileName); Reset(F); Readln(F,s); ShowMessage(s); CloseFile(F); end; //增加 Add Txt procedure TMainForm.Button10Click(Sender: TObject); begin AssignFile(F,FileName); Append(F); Writeln(F,'Three'); Writeln(F,'Four'); CloseFile(F); ShellExecute(0,'Open',PAnsiChar(FileName),nil,nil,SW_SHOW); end; //Read All Txt procedure TMainForm.Button11Click(Sender: TObject); var s: string; begin AssignFile(F,FileName); Reset(F); while not Eof(F) do begin Readln(F,s); Memo1.Lines.Add(s); end; CloseFile(F); end; //Write and Read Not same type 寫入和讀取不同的類型 procedure TMainForm.Button12Click(Sender: TObject); var name: string[6]; age: Word; birthday: TDate; begin AssignFile(F,FileName); Rewrite(F); name := 'lailai '; //????6?才 age := 25; birthday := StrToDate(DateToStr(Now-25*365)); //安さぱ?ネら Writeln(F,name,age,birthday); CloseFile(F); Reset(F); Readln(F,name,age,birthday); Memo1.Clear; Memo1.Lines.Add(name); Memo1.Lines.Add(IntToStr(age)); Memo1.Lines.Add(DateToStr(birthday)); CloseFile(F); end;
4. 結(jié)構(gòu)化文件存取 AssignFile: 關(guān)聯(lián)
type TPersonrec = packed record name: string[12]; age: Word; birthday: TDate; end; //先定義一個結(jié)構(gòu) var DataFile: file of TPersonRec; //聲明 DataFile 用來讀寫 TPersonRec 結(jié)構(gòu)數(shù)據(jù) PersonRec: TPersonRec; //聲明結(jié)構(gòu)變量 FeName: string = 'D:\Delphi Study N\15.Delphi File\b.dat'; //文件名 //write .dat procedure TMainForm.Button13Click(Sender: TObject); begin AssignFile(DataFile,FeName); Rewrite(DataFile); //建立文件, 如果存在就覆蓋 PersonRec.name := 'lailai'; PersonRec.age := 25; PersonRec.birthday := StrToDate('1988/11/16'); write(datafile,personrec); CloseFile(DataFile); ShellExecute(0,'Open',PAnsiChar(FeName),nil,nil,SW_SHOW); end; //Add dat procedure TMainForm.Button14Click(Sender: TObject); begin AssignFile(DataFile,FeName); Reset(DataFile); //Append 只對文本文件 Seek(DataFile,FileSize(DataFile)); //移到文件尾, 這里的 FileSize 表示有多少條記錄 PersonRec.name := 'ailaio'; PersonRec.age := 7; PersonRec.birthday := StrToDate('2006/12/19'); Write(DataFile,PersonRec); CloseFile(DataFile); end; //修改 Alter dat procedure TMainForm.Button15Click(Sender: TObject); begin AssignFile(DataFile,FeName); Reset(DataFile); Seek(DataFile,1); //指針移到第2條 read(datafile,personrec); PersonRec.age := 0; PersonRec.birthday := StrToDate('2013/1/2'); Seek(DataFile,3); write(datafile,personrec); CloseFile(DataFile); end; 5. WinAPI: WritePrivateProfileString、GetPrivateProfileString - 簡單讀寫 Ini 文件 WritePrivateProfileString('段1','Key1','值1', GetPrivateProfileString('段1','Key2','默認(rèn)值',
6.復(fù)雜的結(jié)構(gòu)化存取 有些文檔不是結(jié)構(gòu)化的, 譬如記事本文件; 結(jié)構(gòu)化的檔可以分為以下幾類: procedure TForm1.Button1Click(Sender: TObject); const SourceDir = 'C:\Temp\Folder1';{ 源文件夾必須存在 DestDir = 'C:\Temp\Folder2'; { 如果目標(biāo)文件夾不存在, begin TDirectory.Copy(SourceDir, DestDir); end;
|
|