From 8cff5212f90d44ebc88c999cb235304399012e49 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 30 Sep 2018 11:13:35 +0300 Subject: [PATCH 1/2] update read file test --- .../com/baeldung/file/FileOperationsManualTest.java | 11 ++++++----- core-java-io/src/test/resources/fileTest.txt | 1 + 2 files changed, 7 insertions(+), 5 deletions(-) create mode 100644 core-java-io/src/test/resources/fileTest.txt diff --git a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java index 6a020f5eae..faa6b0ae8d 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java @@ -15,9 +15,11 @@ import java.io.InputStreamReader; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; +import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths; +import java.util.stream.Collectors; import java.util.stream.Stream; import static org.junit.Assert.assertEquals; @@ -50,7 +52,7 @@ public class FileOperationsManualTest { @Test public void givenFileName_whenUsingJarFile_thenFileData() throws IOException { - String expectedData = "BSD License"; + String expectedData = "MIT License"; Class clazz = Matchers.class; InputStream inputStream = clazz.getResourceAsStream("/LICENSE.txt"); @@ -79,7 +81,7 @@ public class FileOperationsManualTest { ClassLoader classLoader = getClass().getClassLoader(); File file = new File(classLoader.getResource("fileTest.txt").getFile()); - String data = FileUtils.readFileToString(file); + String data = FileUtils.readFileToString(file, "UTF-8"); assertEquals(expectedData, data.trim()); } @@ -102,12 +104,11 @@ public class FileOperationsManualTest { Path path = Paths.get(getClass().getClassLoader().getResource("fileTest.txt").toURI()); - StringBuilder data = new StringBuilder(); Stream lines = Files.lines(path); - lines.forEach(line -> data.append(line).append("\n")); + String data = lines.collect(Collectors.joining("\n")); lines.close(); - assertEquals(expectedData, data.toString().trim()); + assertEquals(expectedData, data.trim()); } private String readFromInputStream(InputStream inputStream) throws IOException { diff --git a/core-java-io/src/test/resources/fileTest.txt b/core-java-io/src/test/resources/fileTest.txt new file mode 100644 index 0000000000..ce4bea208b --- /dev/null +++ b/core-java-io/src/test/resources/fileTest.txt @@ -0,0 +1 @@ +Hello World from fileTest.txt!!! \ No newline at end of file From dcf5a40c3fa2f4972b9e119de51a54463e417703 Mon Sep 17 00:00:00 2001 From: Loredana Crusoveanu Date: Sun, 30 Sep 2018 11:14:54 +0300 Subject: [PATCH 2/2] remove import --- .../test/java/com/baeldung/file/FileOperationsManualTest.java | 1 - 1 file changed, 1 deletion(-) diff --git a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java index faa6b0ae8d..e781489808 100644 --- a/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java +++ b/core-java-io/src/test/java/com/baeldung/file/FileOperationsManualTest.java @@ -15,7 +15,6 @@ import java.io.InputStreamReader; import java.net.URISyntaxException; import java.net.URL; import java.net.URLConnection; -import java.nio.charset.Charset; import java.nio.file.Files; import java.nio.file.Path; import java.nio.file.Paths;