commits for BAEL-2076
- Created core-java-io2 module - added resource loading example code - added core-java-io2 to tutorials pom.xml
This commit is contained in:
parent
d585745b4d
commit
88cd75e519
5
core-java-modules/core-java-io2/.gitignore
vendored
Normal file
5
core-java-modules/core-java-io2/.gitignore
vendored
Normal file
@ -0,0 +1,5 @@
|
||||
0.*
|
||||
|
||||
# Files generated by integration tests
|
||||
# *.txt
|
||||
/temp
|
6
core-java-modules/core-java-io2/README.md
Normal file
6
core-java-modules/core-java-io2/README.md
Normal file
@ -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)
|
143
core-java-modules/core-java-io2/pom.xml
Normal file
143
core-java-modules/core-java-io2/pom.xml
Normal file
@ -0,0 +1,143 @@
|
||||
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||
<modelVersion>4.0.0</modelVersion>
|
||||
<artifactId>core-java-io2</artifactId>
|
||||
<version>0.1.0-SNAPSHOT</version>
|
||||
<name>core-java-io2</name>
|
||||
<packaging>jar</packaging>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-java</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-java</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.apache.commons</groupId>
|
||||
<artifactId>commons-collections4</artifactId>
|
||||
<version>${commons-collections4.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>commons-io</groupId>
|
||||
<artifactId>commons-io</artifactId>
|
||||
<version>2.6</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>org.projectlombok</groupId>
|
||||
<artifactId>lombok</artifactId>
|
||||
<version>${lombok.version}</version>
|
||||
<scope>provided</scope>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>log4j</groupId>
|
||||
<artifactId>log4j</artifactId>
|
||||
<version>${log4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency> <!-- needed to bridge to slf4j for projects that use the log4j APIs directly -->
|
||||
<groupId>org.slf4j</groupId>
|
||||
<artifactId>log4j-over-slf4j</artifactId>
|
||||
<version>${org.slf4j.version}</version>
|
||||
</dependency>
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
<build>
|
||||
<finalName>core-java-io2</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<filtering>true</filtering>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
<version>${exec-maven-plugin.version}</version>
|
||||
<configuration>
|
||||
<executable>java</executable>
|
||||
<mainClass>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</mainClass>
|
||||
<arguments>
|
||||
<argument>-Xmx300m</argument>
|
||||
<argument>-XX:+UseParallelGC</argument>
|
||||
<argument>-classpath</argument>
|
||||
<classpath />
|
||||
<argument>com.baeldung.outofmemoryerror.OutOfMemoryGCLimitExceed</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</plugin>
|
||||
|
||||
<plugin>
|
||||
<!-- Build an executable JAR -->
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-jar-plugin</artifactId>
|
||||
<version>${maven-jar-plugin.version}</version>
|
||||
<configuration>
|
||||
<archive>
|
||||
<manifest>
|
||||
<addClasspath>true</addClasspath>
|
||||
<mainClass>com.baeldung.resource.MyResourceLoader</mainClass>
|
||||
</manifest>
|
||||
</archive>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>integration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.codehaus.mojo</groupId>
|
||||
<artifactId>exec-maven-plugin</artifactId>
|
||||
|
||||
<executions>
|
||||
<execution>
|
||||
<id>run-benchmarks</id>
|
||||
<!-- <phase>integration-test</phase> -->
|
||||
<phase>none</phase>
|
||||
<goals>
|
||||
<goal>exec</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<classpathScope>test</classpathScope>
|
||||
<executable>java</executable>
|
||||
<arguments>
|
||||
<argument>-classpath</argument>
|
||||
<classpath />
|
||||
<argument>org.openjdk.jmh.Main</argument>
|
||||
<argument>.*</argument>
|
||||
</arguments>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<properties>
|
||||
|
||||
<!-- util -->
|
||||
<commons-collections4.version>4.1</commons-collections4.version>
|
||||
|
||||
<!-- maven plugins -->
|
||||
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
|
||||
|
||||
</properties>
|
||||
|
||||
</project>
|
@ -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();
|
||||
|
||||
}
|
||||
|
||||
}
|
@ -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.
|
13
core-java-modules/core-java-io2/src/test/resources/.gitignore
vendored
Normal file
13
core-java-modules/core-java-io2/src/test/resources/.gitignore
vendored
Normal file
@ -0,0 +1,13 @@
|
||||
*.class
|
||||
|
||||
#folders#
|
||||
/target
|
||||
/neoDb*
|
||||
/data
|
||||
/src/main/webapp/WEB-INF/classes
|
||||
*/META-INF/*
|
||||
|
||||
# Packaged files #
|
||||
*.jar
|
||||
*.war
|
||||
*.ear
|
1
pom.xml
1
pom.xml
@ -396,6 +396,7 @@
|
||||
<module>core-java-modules/core-java-concurrency-basic</module>
|
||||
<module>core-java-modules/core-java-concurrency-collections</module>
|
||||
<module>core-java-modules/core-java-io</module>
|
||||
<module>core-java-modules/core-java-io2</module>
|
||||
<module>core-java-modules/core-java-nio</module>
|
||||
<module>core-java-modules/core-java-security</module>
|
||||
<module>core-java-modules/core-java-lang-syntax</module>
|
||||
|
Loading…
x
Reference in New Issue
Block a user