forked from eugenp/tutorials
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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
Showing
4 changed files
with
301 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
160 changes: 160 additions & 0 deletions
160
...ore-java-io-6/src/main/java/com/baeldung/filewritervsbufferedwriter/BenchmarkWriters.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} | ||
} |
60 changes: 60 additions & 0 deletions
60
...va-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/BufferedWriterUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |
59 changes: 59 additions & 0 deletions
59
...e-java-io-6/src/test/java/com/baeldung/filewritervsbufferedwriter/FileWriterUnitTest.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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()); | ||
} | ||
} |