Extremely Serious

Category: Java 11

Checking if a Directory is Empty Using Stream

var dirPath = Paths.get("C:\\tmp", "sample"); //The path to be tested.
if (dirPath.toFile().isDirectory()) { //Check if the directory exists.
    try (Stream<Path> entries = Files.list(dirPath)) {
        if (entries.findFirst().isEmpty()) {
            System.out.printf("%s is empty.", dirPath);
        }
    }
}

Module Resolution

Resolve modules from the root module using the following syntax:

java --show-module-resolution -p <MODULE_PATHS> -m <ROOT_MODULE>
Identifier Description
MODULE_PATHS List of modules paths delimited by semi-colon (i.e. ;).
ROOT_MODULE The module to start the resolution.

Sample HttpClient.sendAsync() Method Usage

//Create an instance of HttpClient using its builder.
HttpClient httpClient = HttpClient.newBuilder()
        .version(HttpClient.Version.HTTP_2)
        .build();

//Create an instance of HttpRequest using its builder.
HttpRequest req = HttpRequest.newBuilder(URI.create("https://www.google.com"))
            .GET()
            .build();

/* Use the httpClient.sendAsync() method and encapsulate the response
in the CompletableFuture. */
CompletableFuture<HttpResponse> resFuture =
        httpClient.sendAsync(req, HttpResponse.BodyHandlers.ofString());

//Use the resFuture.thenAccept to wait for the async response.
resFuture.thenAccept(res -> System.out.println(res.version()));

//Wait for the resFuture to to complete.
resFuture.join();