From f5ce4e6d86e5a1e8998d8ce675838f713af02871 Mon Sep 17 00:00:00 2001 From: Michael Olayemi Date: Sun, 29 Oct 2023 05:28:39 +0100 Subject: [PATCH] PrintWriter vs FileWriter in Java (#15034) * https://jira.baeldung.com/browse/BAEL-7117 * PrintWriter vs FileWriter in Java * PrintWriter vs FileWriter in Java * https://jira.baeldung.com/browse/BAEL-7117 * PrintWriter vs FileWriter in Java --- .../core-java-io-apis-2/alabama.txt | 2 + .../core-java-io-apis-2/dream.txt | 1 + .../core-java-io-apis-2/potter.txt | 1 + .../PrintWriterVsFilePrinterUnitTest.java | 64 +++++++++++++++++++ 4 files changed, 68 insertions(+) create mode 100644 core-java-modules/core-java-io-apis-2/alabama.txt create mode 100644 core-java-modules/core-java-io-apis-2/dream.txt create mode 100644 core-java-modules/core-java-io-apis-2/potter.txt create mode 100644 core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwritervsfilewriter/PrintWriterVsFilePrinterUnitTest.java diff --git a/core-java-modules/core-java-io-apis-2/alabama.txt b/core-java-modules/core-java-io-apis-2/alabama.txt new file mode 100644 index 0000000000..d421fa5c33 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/alabama.txt @@ -0,0 +1,2 @@ +I'm going to Alabama +Alabama is a state in the US diff --git a/core-java-modules/core-java-io-apis-2/dream.txt b/core-java-modules/core-java-io-apis-2/dream.txt new file mode 100644 index 0000000000..a4821c7dd4 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/dream.txt @@ -0,0 +1 @@ +Dreams from My Father by Barack Obama \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-2/potter.txt b/core-java-modules/core-java-io-apis-2/potter.txt new file mode 100644 index 0000000000..78974da192 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/potter.txt @@ -0,0 +1 @@ +Harry Potter and the Chamber of Secrets \ No newline at end of file diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwritervsfilewriter/PrintWriterVsFilePrinterUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwritervsfilewriter/PrintWriterVsFilePrinterUnitTest.java new file mode 100644 index 0000000000..6e44ad8318 --- /dev/null +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/printwritervsfilewriter/PrintWriterVsFilePrinterUnitTest.java @@ -0,0 +1,64 @@ +package com.baeldung.printwritervsfilewriter; + +import static org.junit.jupiter.api.Assertions.*; + +import java.io.BufferedReader; +import java.io.File; +import java.io.FileReader; +import java.io.FileWriter; +import java.io.IOException; +import java.io.PrintWriter; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; + +import org.junit.jupiter.api.Test; + +public class PrintWriterVsFilePrinterUnitTest { + + @Test + public void whenWritingToTextFileUsingFileWriter_thenTextMatches() throws IOException { + String result = "Harry Potter and the Chamber of Secrets"; + + File file = new File("potter.txt"); + try (FileWriter fw = new FileWriter(file);) { + fw.write("Harry Potter and the Chamber of Secrets"); + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file));) { + String actualData = reader.readLine(); + assertEquals(result, actualData); + } + } + + @Test + public void whenWritingToTextFileUsingPrintWriterPrintf_thenTextMatches() throws IOException { + String result = "Dreams from My Father by Barack Obama"; + File file = new File("dream.txt"); + try (PrintWriter pw = new PrintWriter(file);) { + String author = "Barack Obama"; + pw.printf("Dreams from My Father by %s", author); + assertTrue(!pw.checkError()); + } + + try (BufferedReader reader = new BufferedReader(new FileReader(file));) { + String actualData = reader.readLine(); + assertEquals(result, actualData); + } + } + + @Test + public void whenWritingToTextFileUsingPrintWriterPrintln_thenTextMatches() throws IOException { + String result = "I'm going to Alabama\nAlabama is a state in the US\n"; + try (PrintWriter pw = new PrintWriter("alabama.txt");) { + pw.println("I'm going to Alabama"); + pw.println("Alabama is a state in the US"); + } + Path path = Paths.get("alabama.txt"); + String actualData = new String(Files.readAllBytes(path)); + assertEquals(result, actualData); + } + +}