BAEL-3215: A Guide to the Java FileReader Class (#7677)

This commit is contained in:
Tapan Avasthi
2019-09-03 04:35:09 +05:30
committed by maibin
parent b0c1efdb7c
commit c6b3e0316c
5 changed files with 402 additions and 0 deletions

View File

@@ -0,0 +1,57 @@
package com.baeldung.filereader;
import java.io.*;
public class FileReaderExample {
public static String readAllCharactersOneByOne(Reader reader) throws IOException {
StringBuilder content = new StringBuilder();
int nextChar;
while ((nextChar = reader.read()) != -1) {
content.append((char) nextChar);
}
return String.valueOf(content);
}
public static String readMultipleCharacters(Reader reader, int length) throws IOException {
char[] buffer = new char[length];
int charactersRead = reader.read(buffer, 0, length);
if (charactersRead != -1) {
return new String(buffer, 0, charactersRead);
} else {
return "";
}
}
public static String readFile(String path) {
FileReader fileReader = null;
try {
fileReader = new FileReader(path);
return readAllCharactersOneByOne(fileReader);
} catch (IOException e) {
e.printStackTrace();
} finally {
if (fileReader != null) {
try {
fileReader.close();
} catch (IOException e) {
e.printStackTrace();
}
}
}
return null;
}
public static String readFileUsingTryWithResources(String path) {
try (FileReader fileReader = new FileReader(path)) {
return readAllCharactersOneByOne(fileReader);
} catch (IOException e) {
e.printStackTrace();
}
return null;
}
}

View File

@@ -0,0 +1,62 @@
package com.baeldung.filereader;
import org.junit.Assert;
import org.junit.Test;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
public class FileReaderExampleUnitTest {
private static final String FILE_PATH = "src/test/resources/HelloWorld.txt";
@Test
public void givenFileReader_whenReadAllCharacters_thenReturnsContent() throws IOException {
String expectedText = "Hello, World!";
File file = new File(FILE_PATH);
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
String content = FileReaderExample.readAllCharactersOneByOne(fileReader);
Assert.assertEquals(expectedText, content);
} finally {
if (fileReader != null) {
fileReader.close();
}
}
}
@Test
public void givenFileReader_whenReadMultipleCharacters_thenReturnsContent() throws IOException {
String expectedText = "Hello";
File file = new File(FILE_PATH);
FileReader fileReader = null;
try {
fileReader = new FileReader(file);
String content = FileReaderExample.readMultipleCharacters(fileReader, 5);
Assert.assertEquals(expectedText, content);
} finally {
if (fileReader != null) {
fileReader.close();
}
}
}
@Test
public void whenReadFile_thenReturnsContent() {
String expectedText = "Hello, World!";
String content = FileReaderExample.readFile(FILE_PATH);
Assert.assertEquals(expectedText, content);
}
@Test
public void whenReadFileUsingTryWithResources_thenReturnsContent() {
String expectedText = "Hello, World!";
String content = FileReaderExample.readFileUsingTryWithResources(FILE_PATH);
Assert.assertEquals(expectedText, content);
}
}

View File

@@ -0,0 +1 @@
Hello, World!