diff --git a/core-java-modules/core-java-io2/.gitignore b/core-java-modules/core-java-io2/.gitignore new file mode 100644 index 0000000000..c61d35324d --- /dev/null +++ b/core-java-modules/core-java-io2/.gitignore @@ -0,0 +1,5 @@ +0.* + +# Files generated by integration tests +# *.txt +/temp \ No newline at end of file diff --git a/core-java-modules/core-java-io2/README.md b/core-java-modules/core-java-io2/README.md new file mode 100644 index 0000000000..d4b417e30e --- /dev/null +++ b/core-java-modules/core-java-io2/README.md @@ -0,0 +1,6 @@ +========= + +## Core Java IO Cookbooks and Examples + +### Relevant Articles: +- [Reading Files Versus Loading Resources](http://www.baeldung.com/reading-files-versus-loading-resources) \ No newline at end of file diff --git a/core-java-modules/core-java-io2/pom.xml b/core-java-modules/core-java-io2/pom.xml new file mode 100644 index 0000000000..8ac7b2975f --- /dev/null +++ b/core-java-modules/core-java-io2/pom.xml @@ -0,0 +1,143 @@ + + 4.0.0 + core-java-io2 + 0.1.0-SNAPSHOT + core-java-io2 + jar + + + com.baeldung + parent-java + 0.0.1-SNAPSHOT + ../../parent-java + + + + + org.apache.commons + commons-collections4 + ${commons-collections4.version} + + + + commons-io + commons-io + 2.6 + + + + org.projectlombok + lombok + ${lombok.version} + provided + + + + log4j + log4j + ${log4j.version} + + + + org.slf4j + log4j-over-slf4j + ${org.slf4j.version} + + + + + + + + + + core-java-io2 + + + 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-jar-plugin + ${maven-jar-plugin.version} + + + + true + com.baeldung.resource.MyResourceLoader + + + + + + + + + + integration + + + + org.codehaus.mojo + exec-maven-plugin + + + + run-benchmarks + + none + + exec + + + test + java + + -classpath + + org.openjdk.jmh.Main + .* + + + + + + + + + + + + + + 4.1 + + + 3.1.0 + + + + \ No newline at end of file diff --git a/core-java-modules/core-java-io2/src/main/java/com/baeldung/resource/MyResourceLoader.java b/core-java-modules/core-java-io2/src/main/java/com/baeldung/resource/MyResourceLoader.java new file mode 100644 index 0000000000..1c5b52e18b --- /dev/null +++ b/core-java-modules/core-java-io2/src/main/java/com/baeldung/resource/MyResourceLoader.java @@ -0,0 +1,42 @@ +package com.baeldung.resource; + +import java.io.BufferedReader; +import java.io.FileReader; +import java.io.IOException; +import java.io.InputStream; +import java.io.InputStreamReader; +import java.util.stream.Collectors; + +public class MyResourceLoader { + + private void loadFileWithReader() throws IOException { + + FileReader fileReader = new FileReader("src/main/resources/input.txt"); + BufferedReader reader = new BufferedReader(fileReader); + String contents = reader.lines() + .collect(Collectors.joining(System.lineSeparator())); + reader.close(); + System.out.println(contents); + + } + + private void loadFileAsResource() throws IOException { + + InputStream inputStream = getClass().getResourceAsStream("/input.txt"); + BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); + String contents = reader.lines() + .collect(Collectors.joining(System.lineSeparator())); + System.out.println(contents); + + } + + public static void main(String[] args) throws IOException { + + MyResourceLoader resourceLoader = new MyResourceLoader(); + + resourceLoader.loadFileWithReader(); + resourceLoader.loadFileAsResource(); + + } + +} diff --git a/core-java-modules/core-java-io2/src/main/resources/input.txt b/core-java-modules/core-java-io2/src/main/resources/input.txt new file mode 100644 index 0000000000..8670fb3460 --- /dev/null +++ b/core-java-modules/core-java-io2/src/main/resources/input.txt @@ -0,0 +1,3 @@ +Lorem ipsum dolor sit amet, consectetur adipiscing elit. +In lacus enim, scelerisque id sapien ut, semper euismod quam. +Nunc ullamcorper semper blandit. \ No newline at end of file diff --git a/core-java-modules/core-java-io2/src/test/resources/.gitignore b/core-java-modules/core-java-io2/src/test/resources/.gitignore new file mode 100644 index 0000000000..83c05e60c8 --- /dev/null +++ b/core-java-modules/core-java-io2/src/test/resources/.gitignore @@ -0,0 +1,13 @@ +*.class + +#folders# +/target +/neoDb* +/data +/src/main/webapp/WEB-INF/classes +*/META-INF/* + +# Packaged files # +*.jar +*.war +*.ear \ No newline at end of file diff --git a/pom.xml b/pom.xml index 3fd1bcf5fb..af5cf4ff03 100644 --- a/pom.xml +++ b/pom.xml @@ -396,6 +396,7 @@ core-java-modules/core-java-concurrency-basic core-java-modules/core-java-concurrency-collections core-java-modules/core-java-io + core-java-modules/core-java-io2 core-java-modules/core-java-nio core-java-modules/core-java-security core-java-modules/core-java-lang-syntax