Skip to content

Commit 37e291b

Browse files
authored
Merge pull request eugenp#5355 from eugenp/file-read-update
update read file test
2 parents ac71273 + dcf5a40 commit 37e291b

2 files changed

Lines changed: 6 additions & 5 deletions

File tree

‎core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java‎

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@
1818
import java.nio.file.Files;
1919
import java.nio.file.Path;
2020
import java.nio.file.Paths;
21+
import java.util.stream.Collectors;
2122
import java.util.stream.Stream;
2223

2324
import static org.junit.Assert.assertEquals;
@@ -50,7 +51,7 @@ public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws
5051

5152
@Test
5253
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
53-
String expectedData = "BSD License";
54+
String expectedData = "MIT License";
5455

5556
Class clazz = Matchers.class;
5657
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
@@ -79,7 +80,7 @@ public void givenFileName_whenUsingFileUtils_thenFileData() throws IOException {
7980

8081
ClassLoader classLoader = getClass().getClassLoader();
8182
File file = new File(classLoader.getResource("fileTest.txt").getFile());
82-
String data = FileUtils.readFileToString(file);
83+
String data = FileUtils.readFileToString(file, "UTF-8");
8384

8485
assertEquals(expectedData, data.trim());
8586
}
@@ -102,12 +103,11 @@ public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException,
102103

103104
Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI());
104105

105-
StringBuilder data = new StringBuilder();
106106
Stream<String> lines = Files.lines(path);
107-
lines.forEach(line -> data.append(line).append("\n"));
107+
String data = lines.collect(Collectors.joining("\n"));
108108
lines.close();
109109

110-
assertEquals(expectedData, data.toString().trim());
110+
assertEquals(expectedData, data.trim());
111111
}
112112

113113
private String readFromInputStream(InputStream inputStream) throws IOException {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Hello World from fileTest.txt!!!

0 commit comments

Comments
 (0)