BAEL-4755: Add Files.list example (#10356)
This commit is contained in:
parent
fc7470b82a
commit
078169109d
@ -24,9 +24,20 @@ public class ListFiles {
|
|||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public Set<String> listFilesUsingFilesList(String dir) throws IOException {
|
||||||
|
try (Stream<Path> stream = Files.list(Paths.get(dir))) {
|
||||||
|
return stream
|
||||||
|
.filter(file -> !Files.isDirectory(file))
|
||||||
|
.map(Path::getFileName)
|
||||||
|
.map(Path::toString)
|
||||||
|
.collect(Collectors.toSet());
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
|
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
|
||||||
try (Stream<Path> stream = Files.walk(Paths.get(dir), depth)) {
|
try (Stream<Path> stream = Files.walk(Paths.get(dir), depth)) {
|
||||||
return stream.filter(file -> !Files.isDirectory(file))
|
return stream
|
||||||
|
.filter(file -> !Files.isDirectory(file))
|
||||||
.map(Path::getFileName)
|
.map(Path::getFileName)
|
||||||
.map(Path::toString)
|
.map(Path::toString)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
|
@ -25,10 +25,15 @@ public class ListFilesUnitTest {
|
|||||||
};
|
};
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenUsingJAVAIO_thenListAllFiles() throws IOException {
|
public void givenDir_whenUsingJAVAIO_thenListAllFiles() {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
|
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenDir_whenUsingFilesList_thenListAllFiles() throws IOException {
|
||||||
|
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFilesList(DIRECTORY));
|
||||||
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
|
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
|
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
|
||||||
|
Loading…
x
Reference in New Issue
Block a user