org.apache.commons.io.FilenameUtils.wildcardMatchOnSystem()方法的使用及代码示例

x33g5p2x  于2022-01-18 转载在 其他  
字(3.9k)|赞(0)|评价(0)|浏览(123)

本文整理了Java中org.apache.commons.io.FilenameUtils.wildcardMatchOnSystem()方法的一些代码示例,展示了FilenameUtils.wildcardMatchOnSystem()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。FilenameUtils.wildcardMatchOnSystem()方法的具体详情如下:
包路径:org.apache.commons.io.FilenameUtils
类名称:FilenameUtils
方法名:wildcardMatchOnSystem

FilenameUtils.wildcardMatchOnSystem介绍

[英]Checks a filename to see if it matches the specified wildcard matcher using the case rules of the system.

The wildcard matcher uses the characters '?' and '*' to represent a single or multiple (zero or more) wildcard characters. This is the same as often found on Dos/Unix command lines. The check is case-sensitive on Unix and case-insensitive on Windows.

wildcardMatch("c.txt", "*.txt")      --> true 
wildcardMatch("c.txt", "*.jpg")      --> false 
wildcardMatch("a/b/c.txt", "a/b/*")  --> true 
wildcardMatch("c.txt", "*.???")      --> true 
wildcardMatch("c.txt", "*.????")     --> false

N.B. the sequence "?" does not work properly at present in match strings.
[中]使用系统的大小写规则检查文件名是否与指定的通配符匹配。
通配符匹配器使用字符“?”和“
”表示一个或多个(零个或多个)通配符。这与通常在Dos/Unix命令行中找到的相同。该检查在Unix上区分大小写,在Windows上不区分大小写。

wildcardMatch("c.txt", "*.txt")      --> true 
wildcardMatch("c.txt", "*.jpg")      --> false 
wildcardMatch("a/b/c.txt", "a/b/*")  --> true 
wildcardMatch("c.txt", "*.???")      --> true 
wildcardMatch("c.txt", "*.????")     --> false

注意顺序“*?”目前无法在匹配字符串中正常工作。

代码示例

代码示例来源:origin: commons-io/commons-io

@Test
public void testMatchOnSystem() {
  assertFalse(FilenameUtils.wildcardMatchOnSystem(null, "Foo"));
  assertFalse(FilenameUtils.wildcardMatchOnSystem("Foo", null));
  assertTrue(FilenameUtils.wildcardMatchOnSystem(null, null));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo", "Foo"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("", ""));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo", "Fo*"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo", "Fo?"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo Bar and Catflap", "Fo*"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("New Bookmarks", "N?w ?o?k??r?s"));
  assertFalse(FilenameUtils.wildcardMatchOnSystem("Foo", "Bar"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo Bar Foo", "F*o Bar*"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Adobe Acrobat Installer", "Ad*er"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo", "*Foo"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("BarFoo", "*Foo"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("Foo", "Foo*"));
  assertTrue(FilenameUtils.wildcardMatchOnSystem("FooBar", "Foo*"));
  assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOO", "*Foo"));
  assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("BARFOO", "*Foo"));
  assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOO", "Foo*"));
  assertEquals(WINDOWS, FilenameUtils.wildcardMatchOnSystem("FOOBAR", "Foo*"));
}

代码示例来源:origin: com.blackducksoftware.integration/hub-common

private Optional<File> getResultFile(final String resultDirectoryName) {
  final File resultDirectory = new File(scanCommand.getOutputDirectory(), resultDirectoryName);
  if (null != resultDirectory && resultDirectory.exists()) {
    final File[] resultFiles = resultDirectory.listFiles((dir, name) -> FilenameUtils.wildcardMatchOnSystem(name, "*.json"));
    if (null != resultFiles && resultFiles.length == 1) {
      return Optional.of(resultFiles[0]);
    }
  }
  logger.error(String.format("Exactly 1 result file was not found in the result directory: %s", resultDirectory.getAbsolutePath()));
  return Optional.empty();
}

相关文章