From 7d485d0e8b4164e1e5ce09e6ffe30d9de8f9ae7a Mon Sep 17 00:00:00 2001 From: Karl Heinz Marbaise Date: Wed, 3 Jan 2024 10:25:43 +0100 Subject: [PATCH] Usage of try-with-resources pom file cleanup --- pom.xml | 24 +++++++++++-------- .../morling/onebrc/CreateMeasurements.java | 19 ++++++--------- 2 files changed, 21 insertions(+), 22 deletions(-) diff --git a/pom.xml b/pom.xml index f54d1f1..d5219ff 100644 --- a/pom.xml +++ b/pom.xml @@ -24,9 +24,8 @@ 1.0.0-SNAPSHOT - 21 true - ${java.version} + 21 UTF-8 UTF-8 @@ -100,11 +99,11 @@ org.apache.maven.plugins maven-compiler-plugin - 3.8.1 + 3.12.1 + true true - --enable-preview --add-modules java.base,jdk.incubator.vector @@ -118,17 +117,17 @@ org.apache.maven.plugins maven-deploy-plugin - 3.0.0-M1 + 3.1.1 org.apache.maven.plugins maven-enforcer-plugin - 3.0.0-M3 + 3.3.0 org.apache.maven.plugins maven-install-plugin - 3.0.0-M1 + 3.1.1 org.apache.maven.plugins @@ -143,12 +142,17 @@ org.apache.maven.plugins maven-site-plugin - 3.9.1 + 3.12.1 org.apache.maven.plugins maven-surefire-plugin - 3.0.0-M5 + 3.2.3 + + + org.apache.maven.plugins + maven-wrapper-plugin + 3.2.0 @@ -223,7 +227,7 @@ - [${java.version},) + ${maven.compiler.release} true diff --git a/src/main/java/dev/morling/onebrc/CreateMeasurements.java b/src/main/java/dev/morling/onebrc/CreateMeasurements.java index 87550e1..eb46663 100644 --- a/src/main/java/dev/morling/onebrc/CreateMeasurements.java +++ b/src/main/java/dev/morling/onebrc/CreateMeasurements.java @@ -16,16 +16,14 @@ package dev.morling.onebrc; import java.io.BufferedWriter; -import java.io.File; -import java.io.FileOutputStream; -import java.io.OutputStreamWriter; -import java.util.Arrays; +import java.nio.file.Files; +import java.nio.file.Path; import java.util.List; import java.util.concurrent.ThreadLocalRandom; public class CreateMeasurements { - private static final String FILE = "./measurements.txt"; + private static final Path MEASUREMENT_FILE = Path.of("./measurements.txt"); private record WeatherStation(String id, double meanTemperature) { double measurement() { @@ -76,7 +74,7 @@ public class CreateMeasurements { // ) // ) TO 'output.csv' (HEADER, DELIMITER ','); // @formatter:on - List stations = Arrays.asList( + List stations = List.of( new WeatherStation("Abha", 18.0), new WeatherStation("Abidjan", 26.0), new WeatherStation("Abéché", 29.4), @@ -491,20 +489,17 @@ public class CreateMeasurements { new WeatherStation("Zanzibar City", 26.0), new WeatherStation("Zürich", 9.3)); - File measurements = new File(FILE); - try (FileOutputStream fos = new FileOutputStream(measurements); BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));) { + try (BufferedWriter bw = Files.newBufferedWriter(MEASUREMENT_FILE)) { for (int i = 0; i < size; i++) { if (i > 0 && i % 50_000_000 == 0) { - System.out.println("Wrote %,d measurements in %s ms".formatted(i, System.currentTimeMillis() - start)); + System.out.printf("Wrote %,d measurements in %s ms%n", i, System.currentTimeMillis() - start); } WeatherStation station = stations.get(ThreadLocalRandom.current().nextInt(stations.size())); bw.write(station.id()); bw.write(";" + station.measurement()); bw.newLine(); } - bw.flush(); - - System.out.println("Created file with %,d measurements in %s ms".formatted(size, System.currentTimeMillis() - start)); } + System.out.printf("Created file with %,d measurements in %s ms%n", size, System.currentTimeMillis() - start); } }