BAEL-5712: Improvement in Listing files under directory (#12643)
Co-authored-by: Harpal Singh <harpal.singh@kaleyra.com>
This commit is contained in:
parent
b8bab5bcbe
commit
cbcf5d4050
|
@ -2,12 +2,7 @@ package com.baeldung.listfiles;
|
||||||
|
|
||||||
import java.io.File;
|
import java.io.File;
|
||||||
import java.io.IOException;
|
import java.io.IOException;
|
||||||
import java.nio.file.DirectoryStream;
|
import java.nio.file.*;
|
||||||
import java.nio.file.FileVisitResult;
|
|
||||||
import java.nio.file.Files;
|
|
||||||
import java.nio.file.Path;
|
|
||||||
import java.nio.file.Paths;
|
|
||||||
import java.nio.file.SimpleFileVisitor;
|
|
||||||
import java.nio.file.attribute.BasicFileAttributes;
|
import java.nio.file.attribute.BasicFileAttributes;
|
||||||
import java.util.HashSet;
|
import java.util.HashSet;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
@ -15,32 +10,31 @@ import java.util.stream.Collectors;
|
||||||
import java.util.stream.Stream;
|
import java.util.stream.Stream;
|
||||||
|
|
||||||
public class ListFiles {
|
public class ListFiles {
|
||||||
public static final int DEPTH = 1;
|
|
||||||
|
|
||||||
public Set<String> listFilesUsingJavaIO(String dir) {
|
public Set<String> listFilesUsingJavaIO(String dir) {
|
||||||
return Stream.of(new File(dir).listFiles())
|
return Stream.of(new File(dir).listFiles())
|
||||||
.filter(file -> !file.isDirectory())
|
.filter(file -> !file.isDirectory())
|
||||||
.map(File::getName)
|
.map(File::getName)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> listFilesUsingFilesList(String dir) throws IOException {
|
public Set<String> listFilesUsingFilesList(String dir) throws IOException {
|
||||||
try (Stream<Path> stream = Files.list(Paths.get(dir))) {
|
try (Stream<Path> stream = Files.list(Paths.get(dir))) {
|
||||||
return stream
|
return stream
|
||||||
.filter(file -> !Files.isDirectory(file))
|
.filter(file -> !Files.isDirectory(file))
|
||||||
.map(Path::getFileName)
|
.map(Path::getFileName)
|
||||||
.map(Path::toString)
|
.map(Path::toString)
|
||||||
.collect(Collectors.toSet());
|
.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
|
return stream
|
||||||
.filter(file -> !Files.isDirectory(file))
|
.filter(file -> !Files.isDirectory(file))
|
||||||
.map(Path::getFileName)
|
.map(Path::getFileName)
|
||||||
.map(Path::toString)
|
.map(Path::toString)
|
||||||
.collect(Collectors.toSet());
|
.collect(Collectors.toSet());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -48,10 +42,9 @@ public class ListFiles {
|
||||||
Set<String> fileList = new HashSet<>();
|
Set<String> fileList = new HashSet<>();
|
||||||
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
|
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
|
||||||
@Override
|
@Override
|
||||||
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
|
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) {
|
||||||
if (!Files.isDirectory(file)) {
|
if (!Files.isDirectory(file)) {
|
||||||
fileList.add(file.getFileName()
|
fileList.add(file.getFileName().toString());
|
||||||
.toString());
|
|
||||||
}
|
}
|
||||||
return FileVisitResult.CONTINUE;
|
return FileVisitResult.CONTINUE;
|
||||||
}
|
}
|
||||||
|
@ -60,16 +53,14 @@ public class ListFiles {
|
||||||
}
|
}
|
||||||
|
|
||||||
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
|
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
|
||||||
Set<String> fileList = new HashSet<>();
|
Set<String> fileSet = new HashSet<>();
|
||||||
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
|
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
|
||||||
for (Path path : stream) {
|
for (Path path : stream) {
|
||||||
if (!Files.isDirectory(path)) {
|
if (!Files.isDirectory(path)) {
|
||||||
fileList.add(path.getFileName()
|
fileSet.add(path.getFileName().toString());
|
||||||
.toString());
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
return fileList;
|
return fileSet;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,21 +1,21 @@
|
||||||
package com.baeldung.listfiles;
|
package com.baeldung.listfiles;
|
||||||
|
|
||||||
import static org.junit.Assert.assertEquals;
|
|
||||||
|
|
||||||
import java.io.IOException;
|
|
||||||
import java.util.HashSet;
|
|
||||||
import java.util.Set;
|
|
||||||
|
|
||||||
import org.junit.Test;
|
import org.junit.Test;
|
||||||
|
|
||||||
import com.baeldung.listfiles.ListFiles;
|
import java.io.IOException;
|
||||||
|
import java.util.Collections;
|
||||||
|
import java.util.HashSet;
|
||||||
|
import java.util.Set;
|
||||||
|
|
||||||
|
import static org.assertj.core.api.AssertionsForClassTypes.assertThat;
|
||||||
|
import static org.junit.Assert.assertEquals;
|
||||||
|
import static org.junit.jupiter.api.Assertions.assertThrows;
|
||||||
|
|
||||||
public class ListFilesUnitTest {
|
public class ListFilesUnitTest {
|
||||||
|
|
||||||
private ListFiles listFiles = new ListFiles();
|
private static final String VALID_DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
|
||||||
private String DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
|
private static final String INVALID_DIRECTORY = "src/test/resources/thisDirectoryDoesNotExist";
|
||||||
private static final int DEPTH = 1;
|
private static final Set<String> EXPECTED_FILE_SET = new HashSet<String>() {
|
||||||
private Set<String> EXPECTED_FILE_LIST = new HashSet<String>() {
|
|
||||||
{
|
{
|
||||||
add("test.xml");
|
add("test.xml");
|
||||||
add("employee.json");
|
add("employee.json");
|
||||||
|
@ -23,29 +23,49 @@ public class ListFilesUnitTest {
|
||||||
add("country.txt");
|
add("country.txt");
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
private static final int DEPTH = 1;
|
||||||
|
private final ListFiles listFiles = new ListFiles();
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenUsingJAVAIO_thenListAllFiles() {
|
public void givenAValidDirectoryWhenUsingJavaIOThenReturnSetOfFileNames() {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
|
assertThat(listFiles.listFilesUsingJavaIO(VALID_DIRECTORY))
|
||||||
|
.isEqualTo(EXPECTED_FILE_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenUsingFilesList_thenListAllFiles() throws IOException {
|
public void givenAInvalidValidDirectoryWhenUsingJavaIOThenThrowsNullPointerExceptino() {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFilesList(DIRECTORY));
|
assertThrows(NullPointerException.class,
|
||||||
|
() -> listFiles.listFilesUsingJavaIO(INVALID_DIRECTORY));
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
|
public void givenAValidDirectoryWhenUsingFilesListThenReturnSetOfFileNames() throws IOException {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
|
assertThat(listFiles.listFilesUsingFilesList(VALID_DIRECTORY))
|
||||||
|
.isEqualTo(EXPECTED_FILE_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenWalkingTreeWithVisitor_thenListAllFiles() throws IOException {
|
public void givenAValidDirectoryWhenUsingFilesWalkWithDepth1ThenReturnSetOfFileNames() throws IOException {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalkAndVisitor(DIRECTORY));
|
assertThat(listFiles.listFilesUsingFileWalk(VALID_DIRECTORY, DEPTH))
|
||||||
|
.isEqualTo(EXPECTED_FILE_SET);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException {
|
public void givenAValidDirectoryWhenUsingFilesWalkFileTreeThenReturnSetOfFileNames() throws IOException {
|
||||||
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingDirectoryStream(DIRECTORY));
|
assertThat(listFiles.listFilesUsingFileWalkAndVisitor(VALID_DIRECTORY))
|
||||||
|
.isEqualTo(EXPECTED_FILE_SET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAValidDirectoryWhenUsingDirectoryStreamThenReturnSetOfFileNames() throws IOException {
|
||||||
|
assertThat(listFiles.listFilesUsingDirectoryStream(VALID_DIRECTORY))
|
||||||
|
.isEqualTo(EXPECTED_FILE_SET);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void givenAValidFileWhenUsingFilesWalkWithDepth1ThenReturnSetOfTheSameFile() throws IOException {
|
||||||
|
Set<String> expectedFileSet = Collections.singleton("test.xml");
|
||||||
|
String filePathString = "src/test/resources/listFilesUnitTestFolder/test.xml";
|
||||||
|
assertEquals(expectedFileSet, listFiles.listFilesUsingFileWalk(filePathString, DEPTH));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue