Java Files With Examples

Java Files With Examples


Files.find() Signature

Review the Files.find() signature.

Files.java


public static Stream<Path> find(Path start,
                                int maxDepth,
                                BiPredicate<Path, BasicFileAttributes> matcher,
                                FileVisitOption... options)
        throws IOException
  • The path, starting file or folder.
  • The maxDepth defined the maximum number of directory levels to search. If we put 1, which means the search for top-level or root folder only, ignore all its subfolders; If we want to search for all folder levels, put Integer.MAX_VALUE.
  • The BiPredicate<Path, BasicFileAttributes> is for condition checking or filtering.
  • The FileVisitOption tells if we want to follow symbolic links, default is no. We can put FileVisitOption.FOLLOW_LINKS to follow symbolic links.

 

 

The Merge Cube made us look at AR in a new way

 

2. Find files by filename

This example uses Files.find() to find files that matched filename google.png, starting from the folder C:\\test, and included all levels of its subfolders.

FileFindExample1.java