Delphi讀取文件和寫入文件總結(jié) --- 讀取文件: 1,關(guān)聯(lián)文件:AssignFile(pMyFile,'c:\ttt.csv'); 2,打開文件:Reset(pMyFile); 3,讀取一行:Readln(pMyFile,pStr); 4,關(guān)閉文件:CloseFile(pMyFile); 示例: procedure TForm1.Button1Click(Sender: TObject); var pMyFile:textfile; pStr : string; begin if OpenDialog1.Execute then begin Assignfile(pMyFile,OpenDialog1.FileName); Reset(pMyFile); while not Eof(pMyFile) do begin Readln(pMyFile,pStr); //fn_DelStock(pStr); //使用讀取的字符串相關(guān)語句 next; end; CloseFile(pMyFile); end; end; +++ 寫入文件: 1,關(guān)聯(lián)文件:AssignFile(pMyFile,'c:\ttt.csv'); 2,打開文件:ReWrite(pMyFile); //如果文件不存在,用ReWrite打開 Append(pMyFile); //如果文件已經(jīng)存在,追加內(nèi)容,用Append打開 3,寫入一行:WriteLn(pMyFile,pStr); 4,關(guān)閉文件:CloseFile(pMyFile); 示例: procedure TForm1.WLog(pMsg: string); var pFObJect,pFPath,pFName: string; pMyFile: textFile; pStr: string; begin pFPath:='d:'; pFName:='StockDel_'+formatDateTime('yyyymmddhhmmss',now); pFObject:=pFPath + '\' + pFName + '.csv'; try AssignFile(pMyFile,pFObject); if FileExists(pFObject)=false then ReWrite(pMyFile) else Append(pMyFile); pStr:=pMsg; WriteLn(pMyFile,pStr); finally CloseFile(pMyFile); end; end; +++ 公用寫日志文件過程 //==ini文件設(shè)置: '日志選項(xiàng)和文件 當(dāng)Log_Flag=N時不記錄,否則均記錄 Log_Flag=1 Log_PathFileName=\\10.105.10.12\c\Prd_220_File\log.dat //==聲明全局變量 x_pLogFile: string; //日志文件名稱 x_pLogFlag: string; //是否記錄日志,N:不寫日志 x_pFindLogFile: boolean; //記錄日志文件是否存在,避免每次寫日志時都要判斷。 //==過程聲明 procedure cpWriteLog(pFObject:string; pTxt:string; pMode:byte); //==初始化全局變量 procedure TForm1.FormCreate(Sender: TObject); begin x_pLogFile:= cfReadIniFile(X_cProgID,'Log_PathFileName','c:\ENRC0300_Log.txt'); x_pLogFlag:= cfReadIniFile(X_cProgID,'Log_Flag','N'); end; //==寫日志過程 procedure cpWriteLog(pFObject:string; pTxt:string; pMode:byte); var pMyFile: textFile; begin if x_pLogFlag='N' then exit; try AssignFile(pMyFile,pFObject); if pMode=1 then pTxt:= DateTimeToStr(Now)+' '+pTxt; if x_pFindLogFile=True then append(pMyFile) else begin if FileExists(pFObject) then append(pMyFile) else reWrite(pMyFile); x_pFindLogFile:=true; end; WriteLn(pMyFile,pTxt); finally CloseFile(pMyFile); end; end; //==調(diào)用過程 x_pMsg:='導(dǎo)出程序['+X_cProgID+']打開,'; cpWriteLog(x_pLogFile,x_pMsg,1); --本文來源于TTT BLOG: http://www./ttt/, 原文地址:http://www./ttt/post/136.html Delphi文件操作所涉及的一些函數(shù) RemoveDirectory //獲取當(dāng)前文件夾 GetCurrentDir //設(shè)置當(dāng)前文件夾 SetCurrentDir; ChDir; SetCurrentDirectory //獲取指定驅(qū)動器的當(dāng)前路徑名 GetDir //文件改名 RenameFile //建立文件夾 CreateDir; CreateDirectory; ForceDirectories //刪除空文件夾 RemoveDir; RemoveDirectory //建立新文件 FileCreate //獲取當(dāng)前文件的版本號 GetFileVersion //獲取磁盤空間 DiskSize; DiskFree //搜索文件 FindFirst; FindNext; FindClose //讀取與設(shè)置文件屬性 FileGetAttr; FileSetAttr //獲取文件的創(chuàng)建時間 FileAge; FileDateToDateTime Delphi代碼 //判斷文件是否存在 FileExists var f: string; begin f := 'c:\temp\test.txt'; if not FileExists(f) then begin //如果文件不存在 end; end; -------------------------------------------------------------------------------- //判斷文件夾是否存在 DirectoryExists var dir: string; begin dir := 'c:\temp'; if not DirectoryExists(dir) then begin //如果文件夾不存在 end; end; -------------------------------------------------------------------------------- //刪除文件 DeleteFile; Windows.DeleteFile var f: string; begin f := 'c:\temp\test.txt'; //DeleteFile(f); //返回 Boolean //或者用系統(tǒng)API: Windows.DeleteFile(PChar(f)); //返回 Boolean end; -------------------------------------------------------------------------------- //刪除文件夾 RemoveDir; RemoveDirectory var dir: string; begin dir := 'c:\temp'; RemoveDir(dir); //返回 Boolean //或者用系統(tǒng) API: RemoveDirectory(PChar(dir)); //返回 Boolean end; -------------------------------------------------------------------------------- //獲取當(dāng)前文件夾 GetCurrentDir var dir: string; begin dir := GetCurrentDir; ShowMessage(dir); //C:\Projects end; -------------------------------------------------------------------------------- //設(shè)置當(dāng)前文件夾 SetCurrentDir; ChDir; SetCurrentDirectory var dir: string; begin dir := 'c:\temp'; if SetCurrentDir(dir) then ShowMessage(GetCurrentDir); //c:\temp //或者 ChDir(dir); //無返回值 //也可以使用API: SetCurrentDirectory(PChar(Dir)); //返回 Boolean end; -------------------------------------------------------------------------------- //獲取指定驅(qū)動器的當(dāng)前路徑名 GetDir var dir: string; b: Byte; begin b := 0; GetDir(b,dir); ShowMessage(dir); // //第一個參數(shù): 1、2、3、4...分別對應(yīng): A、B、C、D... //0 是缺省驅(qū)動器 end; -------------------------------------------------------------------------------- //文件改名 RenameFile var OldName,NewName: string; begin OldName := 'c:\temp\Old.txt'; NewName := 'c:\temp\New.txt'; if RenameFile(OldName,NewName) then ShowMessage('改名成功!'); //也可以: SetCurrentDir('c:\temp'); OldName := 'Old.txt'; NewName := 'New.txt'; if RenameFile(OldName,NewName) then ShowMessage('改名成功!'); end; -------------------------------------------------------------------------------- //建立文件夾 CreateDir; CreateDirectory; ForceDirectories var dir: string; begin dir := 'c:\temp\delphi'; if not DirectoryExists(dir) then CreateDir(dir); //返回 Boolean //也可以直接用API: CreateDirectory(PChar(dir),nil); //返回 Boolean //如果缺少上層目錄將自動補(bǔ)齊: dir := 'c:\temp\CodeGear\Delphi\2007\萬一'; ForceDirectories(dir); //返回 Boolean end; ------------------------------------------------------------------------------- //刪除空文件夾 RemoveDir; RemoveDirectory var dir: string; begin dir := 'c:\temp\delphi'; RemoveDir(dir); //返回 Boolean //也可以直接用API: RemoveDirectory(PChar(dir)); //返回 Boolean end; -------------------------------------------------------------------------------- //建立新文件 FileCreate var FileName: string; i: Integer; begin FileName := 'c:\temp\test.dat'; i := FileCreate(FileName); if i>0 then ShowMessage('新文件的句柄是: ' + IntToStr(i)) else ShowMessage('創(chuàng)建失敗!'); end; -------------------------------------------------------------------------------- //獲取當(dāng)前文件的版本號 GetFileVersion var s: string; i: Integer; begin s := 'C:\WINDOWS\notepad.exe'; i := GetFileVersion(s); //如果沒有版本號返回 -1 ShowMessage(IntToStr(i)); //327681 這是當(dāng)前記事本的版本號(還應(yīng)該再轉(zhuǎn)換一下) end; -------------------------------------------------------------------------------- //獲取磁盤空間 DiskSize; DiskFree var r: Real; s: string; begin r := DiskSize(3); //獲取C:總空間, 單位是字節(jié) r := r/1024/1024/1024; Str(r:0:2,s); //格式為保留兩位小數(shù)的字符串 s := 'C盤總空間是: ' + s + ' GB'; ShowMessage(s); //xx.xx GB r := DiskFree(3); //獲取C:可用空間 r := r/1024/1024/1024; Str(r:0:2,s); s := 'C盤可用空間是: ' + s + ' GB'; ShowMessage(s); //xx.xx GB end; -------------------------------------------------------------------------------- //查找一個文件 FileSearch var FileName,Dir,s: string; begin FileName := 'notepad.exe'; Dir := 'c:\windows'; s := FileSearch(FileName,Dir); if s<>'' then ShowMessage(s) //c:\windows\notepad.exe else ShowMessage('沒找到'); end; -------------------------------------------------------------------------------- //搜索文件 FindFirst; FindNext; FindClose var sr: TSearchRec; //定義 TSearchRec 結(jié)構(gòu)變量 Attr: Integer; //文件屬性 s: string; //要搜索的內(nèi)容 List: TStringList; //存放搜索結(jié)果 begin s := 'c:\windows\*.txt'; Attr := faAnyFile; //文件屬性值faAnyFile表示是所有文件 List := TStringList.Create; //List建立 if FindFirst(s,Attr,sr)=0 then //開始搜索,并給 sr 賦予信息, 返回0表示找到第一個 begin repeat //如果有第一個就繼續(xù)找 List.Add(sr.Name); //用List記下結(jié)果 until(FindNext(sr)<>0); //因?yàn)閟r已經(jīng)有了搜索信息, FindNext只要這一個參數(shù), 返回0表示找到 end; FindClose(sr); //需要結(jié)束搜索, 搜索是內(nèi)含句柄的 ShowMessage(List.Text); //顯示搜索結(jié)果 List.Free; //釋放List end; //更多注釋: //TSearchRec 結(jié)構(gòu)是內(nèi)涵文件大小、名稱、屬性與時間等信息 //TSearchRec 中的屬性是一個整數(shù)值, 可能的值有: //faReadOnly 1 只讀文件 //faHidden 2 隱藏文件 //faSysFile 4 系統(tǒng)文件 //faVolumeID 8 卷標(biāo)文件 //faDirectory 16 目錄文件 //faArchive 32 歸檔文件 //faSymLink 64 鏈接文件 //faAnyFile 63 任意文件 //s 的值也可以使用?通配符,好像只支持7個?, 如果沒有條件就是*, 譬如: C:\* //實(shí)際使用中還應(yīng)該在 repeat 中提些條件, 譬如判斷如果是文件夾就遞歸搜索等等 -------------------------------------------------------------------------------- //讀取與設(shè)置文件屬性 FileGetAttr; FileSetAttr var FileName: string; Attr: Integer; //屬性值是一個整數(shù) begin FileName := 'c:\temp\Test.txt'; Attr := FileGetAttr(FileName); ShowMessage(IntToStr(Attr)); //32, 存檔文件 //設(shè)置為隱藏和只讀文件: Attr := FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN; if FileSetAttr(FileName,Attr)=0 then //返回0表示成功 ShowMessage('設(shè)置成功!'); end; //屬性可選值(有些用不著): //FILE_ATTRIBUTE_READONLY = 1; 只讀 //FILE_ATTRIBUTE_HIDDEN = 2; 隱藏 //FILE_ATTRIBUTE_SYSTEM = 4; 系統(tǒng) //FILE_ATTRIBUTE_DIRECTORY = 16 //FILE_ATTRIBUTE_ARCHIVE = 32; 存檔 //FILE_ATTRIBUTE_DEVICE = 64 //FILE_ATTRIBUTE_NORMAL = 128; 一般 //FILE_ATTRIBUTE_TEMPORARY = 256 //FILE_ATTRIBUTE_SPARSE_FILE = 512 //FILE_ATTRIBUTE_REPARSE_POINT = 1204 //FILE_ATTRIBUTE_COMPRESSED = 2048; 壓縮 //FILE_ATTRIBUTE_OFFLINE = 4096 //FILE_ATTRIBUTE_NOT_CONTENT_INDEXED = 8192; 不被索引 //FILE_ATTRIBUTE_ENCRYPTED = 16384 -------------------------------------------------------------------------------- //獲取文件的創(chuàng)建時間 FileAge; FileDateToDateTime var FileName: string; ti: Integer; dt: TDateTime; begin FileName := 'c:\temp\Test.txt'; ti := FileAge(FileName); ShowMessage(IntToStr(ti)); //返回: 931951472, 需要轉(zhuǎn)換 dt := FileDateToDateTime(ti); //轉(zhuǎn)換 ShowMessage(DateTimeToStr(dt)); //2007-12-12 14:27:32 end; |
|