打开APP
userphoto
未登录

开通VIP,畅享免费电子书等14项超值服

开通VIP
转帖:Delphi读取文件和写入文件总结
Delphi读取文件和写入文件总结
---
读取文件:
1,关联文件:AssignFile(pMyFile,'c:\ttt.csv');
2,打开文件:Reset(pMyFile);
3,读取一行:Readln(pMyFile,pStr);
4,关闭文件: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);  //使用读取的字符串相关语句
            next;
        end;
        CloseFile(pMyFile);
    end;
end; 
+++
写入文件:
1,关联文件:AssignFile(pMyFile,'c:\ttt.csv');
2,打开文件:ReWrite(pMyFile);   //如果文件不存在,用ReWrite打开
             Append(pMyFile);   //如果文件已经存在,追加内容,用Append打开
3,写入一行:WriteLn(pMyFile,pStr);  
4,关闭文件: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文件设置:
'日志选项和文件 当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;
//==调用过程
    x_pMsg:='导出程序['+X_cProgID+']打开,';
    cpWriteLog(x_pLogFile,x_pMsg,1);
 
--本文来源于TTT BLOG: http://www.yoyotao.net/ttt/, 原文地址:http://www.yoyotao.net/ttt/post/136.html
 
 
 
Delphi文件操作所涉及的一些函数 
 
RemoveDirectory
//获取当前文件夹 GetCurrentDir
//设置当前文件夹 SetCurrentDir; ChDir; SetCurrentDirectory
//获取指定驱动器的当前路径名 GetDir
//文件改名 RenameFile
//建立文件夹 CreateDir; CreateDirectory; ForceDirectories
//删除空文件夹 RemoveDir; RemoveDirectory
//建立新文件 FileCreate
//获取当前文件的版本号 GetFileVersion
//获取磁盘空间 DiskSize; DiskFree
//搜索文件 FindFirst; FindNext; FindClose
//读取与设置文件属性 FileGetAttr; FileSetAttr
//获取文件的创建时间 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
//或者用系统API: 
  Windows.DeleteFile(PChar(f)); //返回 Boolean 
end;
-------------------------------------------------------------------------------- 
//删除文件夹 RemoveDir; RemoveDirectory 
var 
  dir: string; 
begin 
  dir := 'c:\temp'; 
  RemoveDir(dir); //返回 Boolean
//或者用系统 API: 
  RemoveDirectory(PChar(dir)); //返回 Boolean 
end;
-------------------------------------------------------------------------------- 
//获取当前文件夹 GetCurrentDir 
var 
  dir: string; 
begin 
  dir := GetCurrentDir; 
  ShowMessage(dir); //C:\Projects 
end;
-------------------------------------------------------------------------------- 
//设置当前文件夹 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;
-------------------------------------------------------------------------------- 
//获取指定驱动器的当前路径名 GetDir 
var 
  dir: string; 
  b: Byte; 
begin 
  b := 0; 
  GetDir(b,dir); 
  ShowMessage(dir); //
//第一个参数: 1、2、3、4...分别对应: A、B、C、D... 
//0 是缺省驱动器 
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
//如果缺少上层目录将自动补齐: 
  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('创建失败!'); 
end;
-------------------------------------------------------------------------------- 
//获取当前文件的版本号 GetFileVersion 
var 
  s: string; 
  i: Integer; 
begin 
  s := 'C:\WINDOWS\notepad.exe'; 
  i := GetFileVersion(s); //如果没有版本号返回 -1 
  ShowMessage(IntToStr(i)); //327681 这是当前记事本的版本号(还应该再转换一下) 
end;
-------------------------------------------------------------------------------- 
//获取磁盘空间 DiskSize; DiskFree 
var 
  r: Real; 
  s: string; 
begin 
  r := DiskSize(3); //获取C:总空间, 单位是字节 
  r := r/1024/1024/1024; 
  Str(r:0:2,s); //格式为保留两位小数的字符串 
  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 结构变量 
  Attr: Integer;     //文件属性 
  s: string;       //要搜索的内容 
  List: TStringList; //存放搜索结果 
begin 
  s := 'c:\windows\*.txt'; 
  Attr := faAnyFile;          //文件属性值faAnyFile表示是所有文件 
  List := TStringList.Create; //List建立
  if FindFirst(s,Attr,sr)=0 then //开始搜索,并给 sr 赋予信息, 返回0表示找到第一个 
  begin 
    repeat                       //如果有第一个就继续找 
      List.Add(sr.Name);       //用List记下结果 
    until(FindNext(sr)<>0);    //因为sr已经有了搜索信息, FindNext只要这一个参数, 返回0表示找到 
  end; 
  FindClose(sr);                 //需要结束搜索, 搜索是内含句柄的
  ShowMessage(List.Text);        //显示搜索结果 
  List.Free;                   //释放List 
end;
//更多注释: 
//TSearchRec 结构是内涵文件大小、名称、属性与时间等信息 
//TSearchRec 中的属性是一个整数值, 可能的值有: 
//faReadOnly 1 只读文件 
//faHidden     2 隐藏文件 
//faSysFile 4 系统文件 
//faVolumeID 8 卷标文件 
//faDirectory 16 目录文件 
//faArchive 32 归档文件 
//faSymLink 64 链接文件 
//faAnyFile 63 任意文件
//s 的值也可以使用?通配符,好像只支持7个?, 如果没有条件就是*, 譬如: C:\* 
//实际使用中还应该在 repeat 中提些条件, 譬如判断如果是文件夹就递归搜索等等
-------------------------------------------------------------------------------- 
//读取与设置文件属性 FileGetAttr; FileSetAttr 
var 
  FileName: string; 
  Attr: Integer; //属性值是一个整数 
begin 
  FileName := 'c:\temp\Test.txt'; 
  Attr := FileGetAttr(FileName); 
  ShowMessage(IntToStr(Attr)); //32, 存档文件
//设置为隐藏和只读文件: 
  Attr := FILE_ATTRIBUTE_READONLY or FILE_ATTRIBUTE_HIDDEN; 
  if FileSetAttr(FileName,Attr)=0 then //返回0表示成功 
    ShowMessage('设置成功!'); 
end;
//属性可选值(有些用不着): 
//FILE_ATTRIBUTE_READONLY = 1; 只读 
//FILE_ATTRIBUTE_HIDDEN = 2; 隐藏 
//FILE_ATTRIBUTE_SYSTEM = 4; 系统 
//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
-------------------------------------------------------------------------------- 
//获取文件的创建时间 FileAge; FileDateToDateTime 
var 
  FileName: string; 
  ti: Integer; 
  dt: TDateTime; 
begin 
  FileName := 'c:\temp\Test.txt'; 
  ti := FileAge(FileName); 
  ShowMessage(IntToStr(ti)); //返回: 931951472, 需要转换
  dt := FileDateToDateTime(ti); //转换 
  ShowMessage(DateTimeToStr(dt)); //2007-12-12 14:27:32 
end;
 
 
 
 
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请点击举报
打开APP,阅读全文并永久保存 查看更多类似文章
猜你喜欢
类似文章
Delphi中如何写操作日志
利用Delphi的File Of Type创建并管理属于你自己的数据库
Delphi 用Record和类型文件做数据库
delphi文件操作(比较全)
Delphi 文件处理(2)
文件与目录的专题
更多类似文章 >>
生活服务
热点新闻
分享 收藏 导长图 关注 下载文章
绑定账号成功
后续可登录账号畅享VIP特权!
如果VIP功能使用有故障,
可点击这里联系客服!

联系客服