Files.walk() and functional interface to remove file extension from fileNames
Transcribed from the original LinkedIn image post.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| import java.io.IOException;
import java.nio.file.*;
import java.util.List;
import java.util.function.Function;
import java.util.stream.Collectors;
import java.util.stream.Stream;
class Day12 {
public static void main(String[] args) throws IOException, InterruptedException {
Function<String, String> extensionRemove = (fileName) -> {
int index = fileName.lastIndexOf(".");
if (index == -1) return "";
return fileName.substring(0, index);
};
List<String> fileNameList;
try (Stream<Path> paths = Files.walk(Paths.get("/home/mohibulhasan/Downloads"))) {
fileNameList = paths
.filter(Files::isRegularFile)
.map(path -> path.getFileName().toString())
.filter(fileName -> !fileName.isEmpty())
.map(extensionRemove)
.collect(Collectors.toList());
}
fileNameList.forEach(System.out::println);
}
}
|
The original LinkedIn graphic is preserved below.
