2015-12-06 11:45:16 -08:00
|
|
|
// files/Find.java
|
2021-01-31 15:42:31 -07:00
|
|
|
// (c)2021 MindView LLC: see Copyright.txt
|
2015-12-06 11:45:16 -08:00
|
|
|
// We make no guarantees that this code is fit for any purpose.
|
2016-09-23 13:23:35 -06:00
|
|
|
// Visit http://OnJava8.com for more book information.
|
2017-05-17 22:06:45 -06:00
|
|
|
// {ExcludeFromGradle}
|
2015-12-06 11:45:16 -08:00
|
|
|
import java.nio.file.*;
|
|
|
|
|
|
|
|
public class Find {
|
2016-01-25 18:05:55 -08:00
|
|
|
public static void
|
|
|
|
main(String[] args) throws Exception {
|
2015-12-06 11:45:16 -08:00
|
|
|
Path test = Paths.get("test");
|
|
|
|
Directories.refreshTestDir();
|
|
|
|
Directories.populateTestDir();
|
|
|
|
// Creating a *directory*, not a file:
|
|
|
|
Files.createDirectory(test.resolve("dir.tmp"));
|
|
|
|
|
|
|
|
PathMatcher matcher = FileSystems.getDefault()
|
|
|
|
.getPathMatcher("glob:**/*.{tmp,txt}");
|
|
|
|
Files.walk(test)
|
|
|
|
.filter(matcher::matches)
|
|
|
|
.forEach(System.out::println);
|
|
|
|
System.out.println("***************");
|
|
|
|
|
|
|
|
PathMatcher matcher2 = FileSystems.getDefault()
|
|
|
|
.getPathMatcher("glob:*.tmp");
|
|
|
|
Files.walk(test)
|
|
|
|
.map(Path::getFileName)
|
|
|
|
.filter(matcher2::matches)
|
|
|
|
.forEach(System.out::println);
|
|
|
|
System.out.println("***************");
|
|
|
|
|
|
|
|
Files.walk(test) // Only look for files
|
|
|
|
.filter(Files::isRegularFile)
|
|
|
|
.map(Path::getFileName)
|
|
|
|
.filter(matcher2::matches)
|
|
|
|
.forEach(System.out::println);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
/* Output:
|
2017-05-10 11:45:39 -06:00
|
|
|
test\bag\foo\bar\baz\5208762845883213974.tmp
|
2015-12-15 11:47:04 -08:00
|
|
|
test\bag\foo\bar\baz\File.txt
|
2017-05-10 11:45:39 -06:00
|
|
|
test\bar\baz\bag\foo\7918367201207778677.tmp
|
2015-12-15 11:47:04 -08:00
|
|
|
test\bar\baz\bag\foo\File.txt
|
2017-05-10 11:45:39 -06:00
|
|
|
test\baz\bag\foo\bar\8016595521026696632.tmp
|
2015-12-15 11:47:04 -08:00
|
|
|
test\baz\bag\foo\bar\File.txt
|
2015-12-06 11:45:16 -08:00
|
|
|
test\dir.tmp
|
2017-05-10 11:45:39 -06:00
|
|
|
test\foo\bar\baz\bag\5832319279813617280.tmp
|
2015-12-15 11:47:04 -08:00
|
|
|
test\foo\bar\baz\bag\File.txt
|
2015-12-06 11:45:16 -08:00
|
|
|
***************
|
2017-05-10 11:45:39 -06:00
|
|
|
5208762845883213974.tmp
|
|
|
|
7918367201207778677.tmp
|
|
|
|
8016595521026696632.tmp
|
2015-12-06 11:45:16 -08:00
|
|
|
dir.tmp
|
2017-05-10 11:45:39 -06:00
|
|
|
5832319279813617280.tmp
|
2015-12-06 11:45:16 -08:00
|
|
|
***************
|
2017-05-10 11:45:39 -06:00
|
|
|
5208762845883213974.tmp
|
|
|
|
7918367201207778677.tmp
|
|
|
|
8016595521026696632.tmp
|
|
|
|
5832319279813617280.tmp
|
2015-12-06 11:45:16 -08:00
|
|
|
*/
|