JAVA-22498 Potential issue in "Difference Between FileReader and BufferedReader in Java"

This commit is contained in:
timis1 2023-06-29 22:04:33 +03:00 committed by n
parent cd6661e117
commit 615bd5c714
2 changed files with 13 additions and 8 deletions

View File

@ -3,18 +3,23 @@ package com.baeldung.bufferedreadervsfilereader;
import static org.junit.jupiter.api.Assertions.assertEquals; import static org.junit.jupiter.api.Assertions.assertEquals;
import java.io.BufferedReader; import java.io.BufferedReader;
import java.io.FileReader; import java.io.File;
import java.io.IOException; 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.junit.jupiter.api.Test;
import org.testng.annotations.Test;
public class BufferedReaderUnitTest { class BufferedReaderUnitTest {
@Test @Test
public void whenReadingAFile_thenReadsLineByLine() { void whenReadingAFile_thenReadsLineByLine() {
StringBuilder result = new StringBuilder(); 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; String line;
while((line = br.readLine()) != null) { while((line = br.readLine()) != null) {

View File

@ -7,10 +7,10 @@ import java.io.IOException;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
public class FileReaderUnitTest { class FileReaderUnitTest {
@Test @Test
public void whenReadingAFile_thenReadsCharByChar() { void whenReadingAFile_thenReadsCharByChar() {
StringBuilder result = new StringBuilder(); StringBuilder result = new StringBuilder();
try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) { try (FileReader fr = new FileReader("src/test/resources/sampleText2.txt")) {