diff --git a/core-java-modules/core-java-io-6/pom.xml b/core-java-modules/core-java-io-6/pom.xml index 62ca4a9b397e..1635ea4a1306 100644 --- a/core-java-modules/core-java-io-6/pom.xml +++ b/core-java-modules/core-java-io-6/pom.xml @@ -21,7 +21,28 @@ + + + + org.apache.maven.plugins + maven-compiler-plugin + ${maven-compiler-plugin.version} + + ${java.version} + ${java.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-generator.version} + + + + + + + - \ No newline at end of file + diff --git a/core-java-modules/core-java-io-6/src/main/java/com/baeldung/filewritervsbufferedwriter/BenchmarkWriters.java b/core-java-modules/core-java-io-6/src/main/java/com/baeldung/filewritervsbufferedwriter/BenchmarkWriters.java new file mode 100644 index 000000000000..48b41523331f --- /dev/null +++ b/core-java-modules/core-java-io-6/src/main/java/com/baeldung/filewritervsbufferedwriter/BenchmarkWriters.java @@ -0,0 +1,160 @@ +package com.baeldung.filewritervsbufferedwriter; + +import org.openjdk.jmh.annotations.*; +import org.slf4j.Logger; +import org.slf4j.LoggerFactory; + +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileWriter; +import java.io.IOException; +import java.net.URL; +import java.net.URLClassLoader; +import java.nio.file.Files; +import java.nio.file.Paths; +import java.util.concurrent.TimeUnit; + +@State(Scope.Benchmark) +@BenchmarkMode(Mode.AverageTime) +@OutputTimeUnit(TimeUnit.MILLISECONDS) +@Warmup(iterations = 5, time = 1, timeUnit = TimeUnit.SECONDS) +@Measurement(iterations = 10, time = 10, timeUnit = TimeUnit.SECONDS) +@Fork(1) +public class BenchmarkWriters { + + private static final Logger log = LoggerFactory.getLogger(BenchmarkWriters.class); + private static final String FILE_PATH = "benchmark.txt"; + private static final String CONTENT = "This is a test line."; + private static final int BUFSIZE = 4194304; // 4MiB + + @Benchmark + public void fileWriter1Write() { + try (FileWriter writer = new FileWriter(FILE_PATH, true)) { + writer.write(CONTENT); + writer.close(); + } catch (IOException e) { + log.error("Error in FileWriter 1 write", e); + } + } + + @Benchmark + public void bufferedWriter1Write() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true), BUFSIZE)) { + writer.write(CONTENT); + writer.close(); + } catch (IOException e) { + log.error("Error in BufferedWriter 1 write", e); + } + } + + @Benchmark + public void fileWriter10Writes() { + try (FileWriter writer = new FileWriter(FILE_PATH, true)) { + for (int i = 0; i < 10; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in FileWriter 10 writes", e); + } + } + + @Benchmark + public void bufferedWriter10Writes() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true), BUFSIZE)) { + for (int i = 0; i < 10; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in BufferedWriter 10 writes", e); + } + } + + @Benchmark + public void fileWriter1000Writes() { + try (FileWriter writer = new FileWriter(FILE_PATH, true)) { + for (int i = 0; i < 1000; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in FileWriter 1000 writes", e); + } + } + + @Benchmark + public void bufferedWriter1000Writes() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true), BUFSIZE)) { + for (int i = 0; i < 1000; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in BufferedWriter 1000 writes", e); + } + } + + @Benchmark + public void fileWriter10000Writes() { + try (FileWriter writer = new FileWriter(FILE_PATH, true)) { + for (int i = 0; i < 10000; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in FileWriter 10000 writes", e); + } + } + + @Benchmark + public void bufferedWriter10000Writes() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true), BUFSIZE)) { + for (int i = 0; i < 10000; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in BufferedWriter 10000 writes", e); + } + } + + @Benchmark + public void fileWriter100000Writes() { + try (FileWriter writer = new FileWriter(FILE_PATH, true)) { + for (int i = 0; i < 100000; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in FileWriter 100000 writes", e); + } + } + + @Benchmark + public void bufferedWriter100000Writes() { + try (BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH, true), BUFSIZE)) { + for (int i = 0; i < 100000; i++) { + writer.write(CONTENT); + } + writer.close(); + } catch (IOException e) { + log.error("Error in BufferedWriter 100000 writes", e); + } + } + + public static void main(String[] args) throws Exception { + Files.deleteIfExists(Paths.get(FILE_PATH)); + + // We need to extract the classpath from the class loader before calling JMH + // This is necessary to load org.openjdk.jmh.runner.ForkedMain + URLClassLoader classLoader = (URLClassLoader) BenchmarkWriters.class.getClassLoader(); + StringBuilder classpath = new StringBuilder(); + for (URL url : classLoader.getURLs()) { + classpath.append(url.getPath()).append(File.pathSeparator); + } + System.setProperty("java.class.path", classpath.toString()); + + org.openjdk.jmh.Main.main(args); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/BufferedWriterUnitTest.java b/core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/BufferedWriterUnitTest.java new file mode 100644 index 000000000000..4e8e65ce3b01 --- /dev/null +++ b/core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/BufferedWriterUnitTest.java @@ -0,0 +1,60 @@ +package com.baeldung.filewritervsbufferedwriter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.BufferedWriter; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class BufferedWriterUnitTest { + + private static final String FILE_PATH = "testBufferedFile.txt"; + + @BeforeEach + void setUp() throws IOException { + Files.deleteIfExists(Paths.get(FILE_PATH)); + } + + @AfterEach + void tearDown() throws IOException { + Files.deleteIfExists(Paths.get(FILE_PATH)); + } + + @Test + void whenWritingUsingBufferedWriter_thenContentIsCorrect() throws IOException { + String content = "Hello, Buffered Baeldung!"; + BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); + writer.write(content); + writer.close(); + + StringBuilder fileContent = new StringBuilder(); + FileReader reader = new FileReader(FILE_PATH); + try { + int ch; + while ((ch = reader.read()) != -1) { + fileContent.append((char) ch); + } + } finally { + reader.close(); + } + + assertEquals(content, fileContent.toString()); + } + + @Test + void whenBufferedFileIsWritten_thenFileExists() throws IOException { + BufferedWriter writer = new BufferedWriter(new FileWriter(FILE_PATH)); + writer.write("Sample buffered content"); + writer.close(); + + File file = new File(FILE_PATH); + assertTrue(file.exists()); + } +} \ No newline at end of file diff --git a/core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/FileWriterUnitTest.java b/core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/FileWriterUnitTest.java new file mode 100644 index 000000000000..86d205710a67 --- /dev/null +++ b/core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/FileWriterUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.filewritervsbufferedwriter; + +import static org.junit.jupiter.api.Assertions.assertEquals; +import static org.junit.jupiter.api.Assertions.assertTrue; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Paths; +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class FileWriterUnitTest { + + private static final String FILE_PATH = "testFile.txt"; + + @BeforeEach + void setUp() throws IOException { + Files.deleteIfExists(Paths.get(FILE_PATH)); + } + + @AfterEach + void tearDown() throws IOException { + Files.deleteIfExists(Paths.get(FILE_PATH)); + } + + @Test + void whenWritingToFile_thenContentIsCorrect() throws IOException { + String content = "Hello, Baeldung!"; + FileWriter writer = new FileWriter(FILE_PATH); + writer.write(content); + writer.close(); + + StringBuilder fileContent = new StringBuilder(); + FileReader reader = new FileReader(FILE_PATH); + try { + int ch; + while ((ch = reader.read()) != -1) { + fileContent.append((char) ch); + } + } finally { + reader.close(); + } + + assertEquals(content, fileContent.toString()); + } + + @Test + void whenFileIsWritten_thenFileExists() throws IOException { + FileWriter writer = new FileWriter(FILE_PATH); + writer.write("Sample content"); + writer.close(); + + File file = new File(FILE_PATH); + assertTrue(file.exists()); + } +} \ No newline at end of file