Delphi 2010 新增功能之: IOUtils 单元(2): TDirectory.TFilterPredicate

本文介绍Delphi中TDirectory.GetFiles函数的使用方法,并演示了如何通过TFilterPredicate参数对搜索到的文件进行过滤处理。提供了使用匿名函数的具体示例。

TDirectory.GetFiles 函数还有一个 TDirectory.TFilterPredicate 类型的参数, 这是个匿名函数类型, 可对搜索结果再处理.


unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Memo1: TMemo;
    Button1: TButton;
    Button2: TButton;
    Button3: TButton;
    Button4: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
    procedure Button3Click(Sender: TObject);
    procedure Button4Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

uses IOUtils, Types;

const path = 'C:\Program Files\Embarcadero\RAD Studio\7.0';

//准备给 TDirectory.GetFiles 调用的函数, 须符合 TDirectory.TFilterPredicate 格式
function fp(const Path: string; const SearchRec: TSearchRec): Boolean;
begin
  Form1.Memo1.Lines.Add(Path + '\' + SearchRec.Name);
end;

procedure TForm1.Button1Click(Sender: TObject);
var
  files: TStringDynArray;
begin
  Memo1.Clear;
  files := TDirectory.GetFiles(path, fp);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  files: TStringDynArray;
begin
  Memo1.Clear;
  files := TDirectory.GetFiles(path, '*.txt', fp);
end;

procedure TForm1.Button3Click(Sender: TObject);
var
  files: TStringDynArray;
begin
  Memo1.Clear;
  files := TDirectory.GetFiles(path, '*.txt', TSearchOption.soAllDirectories, fp);
end;

//可以这样方便地使用匿名函数
procedure TForm1.Button4Click(Sender: TObject);
var
  files: TStringDynArray;
begin
  Memo1.Clear;
  files := TDirectory.GetFiles(path,
    function(const Path: string; const SearchRec: TSearchRec): Boolean
    begin
      Memo1.Lines.Add(Path + '\' + SearchRec.Name);
    end
  );
end;

end.

转载于:https://www.cnblogs.com/del/archive/2009/10/16/1584711.html

javax.script.ScriptException: Sourced file: inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; . . . '' : Typed variable declaration : at Line: 4 : in file: inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; . . . '' : .getValue ( ) Target exception: java.lang.NullPointerException: Attempt to invoke method getValue on null value in inline evaluation of: ``import org.apache.commons.io.IOUtils; import java.util.zip.GZIPOutputStream; . . . '' at line number 4 at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:87) at bsh.engine.BshScriptEngine.eval(BshScriptEngine.java:46) at javax.script.AbstractScriptEngine.eval(AbstractScriptEngine.java:233) at org.apache.jmeter.util.JSR223TestElement.processFileOrScript(JSR223TestElement.java:219) at org.apache.jmeter.modifiers.UTPJSR223PreProcessor.process(UTPJSR223PreProcessor.java:109) at org.apache.jmeter.threads.JMeterThread.runPreProcessors(JMeterThread.java:965) at org.apache.jmeter.threads.JMeterThread.executeSamplePackage(JMeterThread.java:549) at org.apache.jmeter.threads.JMeterThread.processSampler(JMeterThread.java:489) at org.apache.jmeter.threads.JMeterThread.run(JMeterThread.java:256) at java.lang.Thread.run(Thread.java:748) Caused by: java.lang.NullPointerException: Attempt to invoke method getValue on null value at bsh.Reflect.resolveExpectedJavaMethod(Reflect.java:378) at bsh.Reflect.invokeObjectMethod(Reflect.java:74) at bsh.BSHPrimarySuffix.doName(BSHPrimarySuffix.java:170) at bsh.BSHPrimarySuffix.doSuffix(BSHPrimarySuffix.java:114) at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:74) at bsh.BSHPrimaryExpression.eval(BSHPrimaryExpression.java:41) at bsh.BSHVariableDeclarator.eval(BSHVariableDeclarator.java:80) at bsh.BSHTypedVariableDeclaration.eval(BSHTypedVariableDeclaration.java:78) at bsh.Interpreter.eval(Interpreter.java:659) at bsh.Interpreter.eval(Interpreter.java:750) at bsh.Interpreter.eval(Interpreter.java:739) at bsh.engine.BshScriptEngine.evalSource(BshScriptEngine.java:78) ... 9 more
最新发布
08-28
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值