码迷,mamicode.com
首页 > 其他好文 > 详细

日志类(一)文件管理

时间:2015-09-16 17:27:54      阅读:187      评论:0      收藏:0      [点我收藏+]

标签:

文件的,搜索,创建,删除,排序


 1   { *
 2     文件类
 3     * }
 4   TPathFile = record
 5   public
 6     // 补全全路径
 7     class function FullPath(Path: string): string; static;
 8     // 搜索指定类型文件
 9     class function SearthFile(Path: string; const Wildcard: string;
10       const allDir: Boolean): TStringDynArray; static;
11 
12     // 排序数组
13     class function MakeIComparer(ComparerFunc: TComparison<string>)
14       : IComparer<string>; static;
15     class procedure Sort(var Arr: TStringDynArray); overload; static;
16     class procedure Sort(var Arr: TStringDynArray;
17       ICompareFunc: IComparer<string>); overload; static;
18     // 删除指定文件
19     class function DeleteFile(Arr: TStringDynArray; Start, Stop: Integer)
20       : Integer; static;
21     // 生成指定文件
22     class function CreateFile(PathFile: string): TFileStream; static;
23   end;
24 
25 { 实现如下 }
26 
27 { TFile }
28 
29 class function TPathFile.CreateFile(PathFile: string): TFileStream;
30 begin
31   Result := TFile.Create(PathFile);
32 end;
33 
34 class function TPathFile.DeleteFile(Arr: TStringDynArray;
35   Start, Stop: Integer): Integer;
36 var
37   TempInt: Integer;
38 begin
39   Result := 0;
40   if Start > Stop then
41     Exit;
42   for TempInt := Start to Stop do
43   begin
44     try
45       TFile.Delete(Arr[TempInt]);
46     except
47       Dec(Result);
48     end;
49     Inc(Result);
50   end;
51 end;
52 
53 class function TPathFile.FullPath(Path: string): string;
54 begin
55   if TPath.IsPathRooted(Path) then
56     Result := Path
57   else
58     Result := TPath.GetFullPath(Path);
59 end;
60 
61 class function TPathFile.MakeIComparer(ComparerFunc: TComparison<string>)
62   : IComparer<string>;
63 begin
64   Result := TComparer<string>.Construct(ComparerFunc);
65 end;
66 
67 class function TPathFile.SearthFile(Path: string; const Wildcard: string;
68   const allDir: Boolean): TStringDynArray;
69 begin
70   if allDir then
71   begin
72     Result := TDirectory.GetFiles(Path, Wildcard,
73       TSearchOption.soAllDirectories);
74   end
75   else
76     Result := TDirectory.GetFileSystemEntries(Path, Wildcard);
77 end;
78 
79 class procedure TPathFile.Sort(var Arr: TStringDynArray);
80 begin
81   TArray.Sort<string>(Arr);
82 end;
83 
84 class procedure TPathFile.Sort(var Arr: TStringDynArray;
85   ICompareFunc: IComparer<string>);
86 begin
87   TArray.Sort<string>(Arr, ICompareFunc);
88 end;

 

日志类(一)文件管理

标签:

原文地址:http://www.cnblogs.com/Study-Pascal/p/4813702.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!