Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
* Update pom.xml

* Create createdir.txt

* Add files via upload

* Delete core-java-modules/core-java-io-6/src/main/java/com/baeldung/filewritervsbufferedwriter/createdir.txt

* Create createdir.txt

* Add files via upload

* Delete core-java-modules/core-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/createdir.txt

* Update pom.xml

* REVERT Update pom.xml
  • Loading branch information
jsfan3 authored Jun 30, 2024
1 parent 1bd4dd4 commit 945309d
Show file tree
Hide file tree
Showing 4 changed files with 301 additions and 1 deletion.
23 changes: 22 additions & 1 deletion core-java-modules/core-java-io-6/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,28 @@
</dependency>
</dependencies>

<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-compiler-plugin</artifactId>
<version>${maven-compiler-plugin.version}</version>
<configuration>
<source>${java.version}</source>
<target>${java.version}</target>
<annotationProcessorPaths>
<path>
<groupId>org.openjdk.jmh</groupId>
<artifactId>jmh-generator-annprocess</artifactId>
<version>${jmh-generator.version}</version>
</path>
</annotationProcessorPaths>
</configuration>
</plugin>
</plugins>
</build>

<properties>

</properties>
</project>
</project>
Original file line number Diff line number Diff line change
@@ -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);
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}
Original file line number Diff line number Diff line change
@@ -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());
}
}

0 comments on commit 945309d

Please sign in to comment.