60 lines
1.7 KiB
Java
Raw Normal View History

2015-12-06 11:45:16 -08:00
// files/Find.java
2015-12-15 11:47:04 -08:00
// (c)2016 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.
// Visit http://mindviewinc.com/Books/OnJava/ for more book information.
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:
2015-12-15 11:47:04 -08:00
test\bag\foo\bar\baz\7875645296823748474.tmp
test\bag\foo\bar\baz\File.txt
test\bar\baz\bag\foo\8495339501512512468.tmp
test\bar\baz\bag\foo\File.txt
test\baz\bag\foo\bar\4657930969137717517.tmp
test\baz\bag\foo\bar\File.txt
2015-12-06 11:45:16 -08:00
test\dir.tmp
2015-12-15 11:47:04 -08:00
test\foo\bar\baz\bag\5205870604020775563.tmp
test\foo\bar\baz\bag\File.txt
2015-12-06 11:45:16 -08:00
***************
2015-12-15 11:47:04 -08:00
7875645296823748474.tmp
8495339501512512468.tmp
4657930969137717517.tmp
2015-12-06 11:45:16 -08:00
dir.tmp
2015-12-15 11:47:04 -08:00
5205870604020775563.tmp
2015-12-06 11:45:16 -08:00
***************
2015-12-15 11:47:04 -08:00
7875645296823748474.tmp
8495339501512512468.tmp
4657930969137717517.tmp
5205870604020775563.tmp
2015-12-06 11:45:16 -08:00
*/