Close

Java IO & NIO - Files.find() Examples

Java IO & NIO Java Java API 


Class:

java.nio.file.Files

java.lang.Objectjava.lang.Objectjava.nio.file.Filesjava.nio.file.FilesLogicBig

Method:

public static Stream<Path> find(Path start,
                                int maxDepth,
                                BiPredicate<Path,BasicFileAttributes> matcher,
                                FileVisitOption... options)
                         throws IOException

This method searches for files in a file tree rooted at a given starting file path.

Parameters:
start - the starting file
maxDepth - the maximum number of directory levels to search
matcher - the function used to decide whether a file should be included in the returned stream
options - options to configure the traversal
Returns:
An instance of java.util.stream.Stream having matched paths (java.nio.file.Path).

Examples


c:\test

c:\test>tree c:\test /F /A
Folder PATH listing for volume OS_Install
Volume serial number is 7844-2D90
C:\TEST
\---f1
+---f2
| f2.txt
| items.txt
|
\---f3
| random-items.txt
|
\---f5
old-file.txt

package com.logicbig.example.files;

import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.stream.Stream;

public class FindExample {

public static void main(String... args) throws IOException {
Path testPath = Paths.get("c:\\test");
//finding files containing 'items' in name
Stream<Path> stream =
Files.find(testPath, 100,
(path, basicFileAttributes) -> {
File file = path.toFile();
return !file.isDirectory() &&
file.getName().contains("items");
});
stream.forEach(System.out::println);
}
}

Output

c:\test\f1\f2\items.txt
c:\test\f1\f3\random-items.txt




c:\test

c:\test>tree c:\test /F /A
Folder PATH listing for volume OS_Install
Volume serial number is 000000E6 7844:2D90
C:\TEST
\---f1
+---f2
| f2.txt
| items.txt
|
\---f3
| random-items.txt
|
\---f5
old-file.txt

package com.logicbig.example.files;

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.attribute.BasicFileAttributes;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.util.stream.Stream;

public class FindExample2 {

public static void main(String... args) throws IOException {
Path testPath = Paths.get("c:\\test");
LocalDateTime target = LocalDate.of(2016, 1, 1).atStartOfDay();
//finding files which were created before target date
Stream<Path> stream = Files.find(testPath, 100,
(path, basicFileAttributes) -> {
try {
LocalDateTime created = getCreationDateTime(basicFileAttributes);
return created.isBefore(target);
} catch (IOException e) {
e.printStackTrace();
}
return false;
});

stream.forEach((p) -> {
System.out.println(p);
try {
BasicFileAttributes attr = Files.readAttributes(p,
BasicFileAttributes.class);
System.out.println("date created :" + getCreationDateTime(attr));
} catch (IOException e) {
e.printStackTrace();
}
});
}

public static LocalDateTime getCreationDateTime(BasicFileAttributes attr)
throws IOException {

return attr.creationTime()
.toInstant()
.atZone(ZoneId.systemDefault())
.toLocalDateTime();
}
}

Output

c:\test\f1\f3\f5\old-file.txt
date created :2015-12-14T14:29




See Also