List files within directory

Issue: BAEL-2447
This commit is contained in:
PranayVJain 2019-01-27 11:51:13 +05:30 committed by Josh Cummings
parent 6a2dc09fb6
commit 9a567f95b4
6 changed files with 114 additions and 0 deletions

View File

@ -0,0 +1,64 @@
package com.baeldung.files;
import java.io.File;
import java.io.IOException;
import java.nio.file.DirectoryStream;
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.util.HashSet;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.Stream;
public class ListFiles {
public static final int DEPTH = 1;
public Set<String> listFilesUsingJavaIO(String dir) {
return Stream.of(new File(dir).listFiles())
.filter(file -> !file.isDirectory())
.map(File::getName)
.collect(Collectors.toSet());
}
public Set<String> listFilesUsingFileWalk(String dir, int depth) throws IOException {
try (Stream<Path> stream = Files.walk(Paths.get(dir), depth)) {
return stream.filter(file -> !Files.isDirectory(file))
.map(Path::getFileName)
.map(Path::toString)
.collect(Collectors.toSet());
}
}
public Set<String> listFilesUsingFileWalkAndVisitor(String dir) throws IOException {
Set<String> fileList = new HashSet<>();
Files.walkFileTree(Paths.get(dir), new SimpleFileVisitor<Path>() {
@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs) throws IOException {
if (!Files.isDirectory(file)) {
fileList.add(file.getFileName()
.toString());
}
return FileVisitResult.CONTINUE;
}
});
return fileList;
}
public Set<String> listFilesUsingDirectoryStream(String dir) throws IOException {
Set<String> fileList = new HashSet<>();
try (DirectoryStream<Path> stream = Files.newDirectoryStream(Paths.get(dir))) {
for (Path path : stream) {
if (!Files.isDirectory(path)) {
fileList.add(path.getFileName()
.toString());
}
}
}
return fileList;
}
}

View File

@ -0,0 +1,46 @@
package com.baeldung.file;
import static org.junit.Assert.assertEquals;
import java.io.IOException;
import java.util.HashSet;
import java.util.Set;
import org.junit.Test;
import com.baeldung.files.ListFiles;
public class ListFilesUnitTest {
private ListFiles listFiles = new ListFiles();
private String DIRECTORY = "src/test/resources/listFilesUnitTestFolder";
private static final int DEPTH = 1;
private Set<String> EXPECTED_FILE_LIST = new HashSet<String>() {
{
add("test.xml");
add("employee.json");
add("students.json");
add("country.txt");
}
};
@Test
public void givenDir_whenUsingJAVAIO_thenListAllFiles() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingJavaIO(DIRECTORY));
}
@Test
public void givenDir_whenWalkingTree_thenListAllFiles() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalk(DIRECTORY,DEPTH));
}
@Test
public void givenDir_whenWalkingTreeWithVisitor_thenListAllFiles() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingFileWalkAndVisitor(DIRECTORY));
}
@Test
public void givenDir_whenUsingDirectoryStream_thenListAllFiles() throws IOException {
assertEquals(EXPECTED_FILE_LIST, listFiles.listFilesUsingDirectoryStream(DIRECTORY));
}
}

View File

@ -0,0 +1 @@
This is a sample txt file for unit test ListFilesUnitTest

View File

@ -0,0 +1 @@
<xml></xml>