Skip to content

Commit 8cff521

Browse files
committed
update read file test
1 parent ac71273 commit 8cff521

2 files changed

Lines changed: 7 additions & 5 deletions

File tree

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

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -15,9 +15,11 @@
1515
import java.net.URISyntaxException;
1616
import java.net.URL;
1717
import java.net.URLConnection;
18+
import java.nio.charset.Charset;
1819
import java.nio.file.Files;
1920
import java.nio.file.Path;
2021
import java.nio.file.Paths;
22+
import java.util.stream.Collectors;
2123
import java.util.stream.Stream;
2224

2325
import static org.junit.Assert.assertEquals;
@@ -50,7 +52,7 @@ public void givenFileNameAsAbsolutePath_whenUsingClasspath_thenFileData() throws
5052

5153
@Test
5254
public void givenFileName_whenUsingJarFile_thenFileData() throws IOException {
53-
String expectedData = "BSD License";
55+
String expectedData = "MIT License";
5456

5557
Class clazz = Matchers.class;
5658
InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt");
@@ -79,7 +81,7 @@ public void givenFileName_whenUsingFileUtils_thenFileData() throws IOException {
7981

8082
ClassLoader classLoader = getClass().getClassLoader();
8183
File file = new File(classLoader.getResource("fileTest.txt").getFile());
82-
String data = FileUtils.readFileToString(file);
84+
String data = FileUtils.readFileToString(file, "UTF-8");
8385

8486
assertEquals(expectedData, data.trim());
8587
}
@@ -102,12 +104,11 @@ public void givenFilePath_whenUsingFilesLines_thenFileData() throws IOException,
102104

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

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

110-
assertEquals(expectedData, data.toString().trim());
111+
assertEquals(expectedData, data.trim());
111112
}
112113

113114
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)