From 615bd5c714749fd814812721c63808fc63b99463 Mon Sep 17 00:00:00 2001 From: timis1 Date: Thu, 29 Jun 2023 22:04:33 +0300 Subject: [PATCH] JAVA-22498 Potential issue in "Difference Between FileReader and BufferedReader in Java" --- .../BufferedReaderUnitTest.java | 17 +++++++++++------ .../FileReaderUnitTest.java | 4 ++-- 2 files changed, 13 insertions(+), 8 deletions(-) diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java index 06b7d38821..62f0d9da22 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/BufferedReaderUnitTest.java @@ -3,18 +3,23 @@ package com.baeldung.bufferedreadervsfilereader; import static org.junit.jupiter.api.Assertions.assertEquals; import java.io.BufferedReader; -import java.io.FileReader; +import java.io.File; import java.io.IOException; +import java.io.InputStreamReader; +import java.nio.charset.StandardCharsets; +import java.nio.file.Files; +import java.nio.file.Path; -import org.junit.jupiter.api.Assertions; -import org.testng.annotations.Test; -public class BufferedReaderUnitTest { +import org.junit.jupiter.api.Test; + +class BufferedReaderUnitTest { @Test - public void whenReadingAFile_thenReadsLineByLine() { + void whenReadingAFile_thenReadsLineByLine() { StringBuilder result = new StringBuilder(); - try (BufferedReader br = new BufferedReader(new FileReader("src/test/resources/sampleText1.txt"))) { + final Path filePath = new File("src/test/resources/sampleText1.txt").toPath(); + try (BufferedReader br = new BufferedReader(new InputStreamReader(Files.newInputStream(filePath), StandardCharsets.UTF_8))) { String line; while((line = br.readLine()) != null) { diff --git a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java index 5df870e7b5..da724d32e8 100644 --- a/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java +++ b/core-java-modules/core-java-io-apis-2/src/test/java/com/baeldung/bufferedreadervsfilereader/FileReaderUnitTest.java @@ -7,10 +7,10 @@ import java.io.IOException; import org.junit.jupiter.api.Test; -public class FileReaderUnitTest { +class FileReaderUnitTest { @Test - public void whenReadingAFile_thenReadsCharByChar() { + void whenReadingAFile_thenReadsCharByChar() { StringBuilder result = new StringBuilder(); try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {