如何在Groovy中区分FileFilter和FilenameFilter?

时间:2017-01-18 10:07:23

标签: groovy lambda

我想列出某些类型的文件并写道:

File[] imageFiles = srcDir.listFiles({ File file ->
            file().name.endsWith("Artboard1ldpi.png") ||
            file().name.endsWith("Artboard1mdpi.png") ||
            file().name.endsWith("Artboard1hdpi.png") ||
            file().name.endsWith("Artboard1xhdpi.png") ||
            file().name.endsWith("Artboard1xxhdpi.png") ||
            file().name.endsWith("Artboard1xxxhdpi.png")
        })

不幸的是,我收到以下错误:

> Ambiguous method overloading for method java.io.File#listFiles.
Cannot resolve which method to invoke for [class CLOSURENAME] due to overlapping prototypes between:
    [interface java.io.FileFilter]
    [interface java.io.FilenameFilter]

如何提示Groovy,选择什么?

2 个答案:

答案 0 :(得分:5)

File[] imageFiles = srcDir.listFiles({ File file ->
    file.name.endsWith("Artboard1ldpi.png") ||
    file.name.endsWith("Artboard1mdpi.png") ||
    file.name.endsWith("Artboard1hdpi.png") ||
    file.name.endsWith("Artboard1xhdpi.png") ||
    file.name.endsWith("Artboard1xxhdpi.png") ||
    file.name.endsWith("Artboard1xxxhdpi.png")
} as FileFilter)

答案 1 :(得分:4)

这也应该有用,DRY;)

File[] imageFiles = srcDir.listFiles({ File file ->
   [
      "Artboard1ldpi.png",
      "Artboard1ldpi.png",
      "Artboard1mdpi.png",
      "Artboard1hdpi.png",
      "Artboard1xhdpi.png",
      "Artboard1xxhdpi.png",
      "Artboard1xxxhdpi.png",
   ].any { file.name.endsWith(it) }
} as FileFilter)