diff --git a/core-java-modules/core-java-io-2/.gitignore b/core-java-modules/core-java-io-2/.gitignore new file mode 100644 index 0000000000..de044ef20f --- /dev/null +++ b/core-java-modules/core-java-io-2/.gitignore @@ -0,0 +1,3 @@ +# Intellij +.idea/ +*.iml diff --git a/core-java-modules/core-java-io-2/pom.xml b/core-java-modules/core-java-io-2/pom.xml new file mode 100644 index 0000000000..64d9434beb --- /dev/null +++ b/core-java-modules/core-java-io-2/pom.xml @@ -0,0 +1,279 @@ + + 4.0.0 + core-java-io-2 + 0.1.0-SNAPSHOT + core-java-io-2 + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + + + + + net.sourceforge.collections + collections-generic + ${collections-generic.version} + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + commons-io + commons-io + ${commons-io.version} + + + org.apache.commons + commons-lang3 + ${commons-lang3.version} + + + org.apache.commons + commons-math3 + ${commons-math3.version} + + + org.decimal4j + decimal4j + ${decimal4j.version} + + + org.bouncycastle + bcprov-jdk15on + ${bouncycastle.version} + + + org.unix4j + unix4j-command + ${unix4j.version} + + + com.googlecode.grep4j + grep4j + ${grep4j.version} + + + + + com.fasterxml.jackson.core + jackson-databind + ${jackson.version} + + + + log4j + log4j + ${log4j.version} + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + org.assertj + assertj-core + ${assertj.version} + test + + + com.jayway.awaitility + awaitility + ${avaitility.version} + test + + + commons-codec + commons-codec + ${commons-codec.version} + + + org.javamoney + moneta + ${moneta.version} + + + org.owasp.esapi + esapi + ${esapi.version} + + + com.sun.messaging.mq + fscontext + ${fscontext.version} + + + com.codepoetics + protonpack + ${protonpack.version} + + + one.util + streamex + ${streamex.version} + + + io.vavr + vavr + ${vavr.version} + + + org.openjdk.jmh + jmh-core + ${jmh-core.version} + + + org.openjdk.jmh + jmh-generator-annprocess + ${jmh-generator-annprocess.version} + + + org.hsqldb + hsqldb + ${hsqldb.version} + runtime + + + + org.asynchttpclient + async-http-client + ${async-http-client.version} + + + com.opencsv + opencsv + ${opencsv.version} + test + + + + org.apache.tika + tika-core + ${tika.version} + + + net.sf.jmimemagic + jmimemagic + ${jmime-magic.version} + + + + + core-java-io-2 + + + src/main/resources + true + + + + + org.codehaus.mojo + exec-maven-plugin + ${exec-maven-plugin.version} + + java + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + -Xmx300m + -XX:+UseParallelGC + -classpath + + com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed + + + + + org.apache.maven.plugins + maven-javadoc-plugin + ${maven-javadoc-plugin.version} + + ${maven.compiler.source} + ${maven.compiler.target} + + + + + + + + integration + + + + org.codehaus.mojo + exec-maven-plugin + + + + run-benchmarks + + none + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + + + + + + + + + + 1.55 + 1.10 + 3.6.1 + 1.0.3 + 4.1 + 4.01 + 0.4 + 1.8.7 + 4.6-b01 + 1.13 + 0.6.5 + 0.9.0 + 4.1 + + 3.6.1 + 1.7.0 + + + 3.0.0-M1 + 2.4.0 + 2.1.0.1 + 1.19 + 2.4.5 + + 1.18 + 0.1.5 + + + \ No newline at end of file diff --git a/core-java-modules/core-java-io-2/src/main/java/com/baeldung/filereader/FileReaderExample.java b/core-java-modules/core-java-io-2/src/main/java/com/baeldung/filereader/FileReaderExample.java new file mode 100644 index 0000000000..34a8f61615 --- /dev/null +++ b/core-java-modules/core-java-io-2/src/main/java/com/baeldung/filereader/FileReaderExample.java @@ -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; + } + + +} \ No newline at end of file diff --git a/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java new file mode 100644 index 0000000000..968c041115 --- /dev/null +++ b/core-java-modules/core-java-io-2/src/test/java/com/baeldung/filereader/FileReaderExampleUnitTest.java @@ -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); + } + +} diff --git a/core-java-modules/core-java-io-2/src/test/resources/HelloWorld.txt b/core-java-modules/core-java-io-2/src/test/resources/HelloWorld.txt new file mode 100644 index 0000000000..b45ef6fec8 --- /dev/null +++ b/core-java-modules/core-java-io-2/src/test/resources/HelloWorld.txt @@ -0,0 +1 @@ +Hello, World! \ No newline at end of file