Stream.peek to get debug information and log it.

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
28
29
30
31
32
33
34
35
36
37
38
39
40
import java.io.File;
import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.Date;
import java.util.List;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

class Day19 {
    public static final Logger logger = LoggerFactory.getLogger(Day19.class);

    private static void logFileMetadata(File file) {
        Path path = Path.of(file.getAbsolutePath());
        BasicFileAttributes basicFileAttributes = null;
        try {
            basicFileAttributes = Files.readAttributes(path, BasicFileAttributes.class);
        } catch (IOException e) {
            e.printStackTrace();
        }

        logger.info(
            "File Name is {}\n Creation Time {}\n Size {}\n Last modified {}",
            file.getName(),
            new Date(basicFileAttributes.creationTime().toMillis()),
            basicFileAttributes.size() / 1024,
            new Date(basicFileAttributes.lastModifiedTime().toMillis())
        );
    }

    public static void main(String[] args) {
        List<File> selectedFilesForUpload = getFileList("/home/mohibulhasan/Downloads");
        selectedFilesForUpload.stream()
            .filter(File::exists)
            .map(File::getAbsoluteFile)
            .peek(Day19::logFileMetadata)
            .forEach(file -> FileUploadService.upload(file));
    }
}

The original LinkedIn graphic is preserved below.

Day 19 LinkedIn post