BAEL-2076: loading resources (#7347)
* commits for BAEL-2076 - Created core-java-io2 module - added resource loading example code - added core-java-io2 to tutorials pom.xml * Committing review changes - change to try-with-resources - change the order of main method * Reformatting try-with-resources * Moving resources example from core-java-io2 to core-java-io. Removing core-java-io2 * Removing stray core-java-io2 in tutorials pom file.
This commit is contained in:
parent
cf2d93b4e7
commit
c85be3d29d
@ -207,6 +207,21 @@
|
|||||||
<target>${maven.compiler.target}</target>
|
<target>${maven.compiler.target}</target>
|
||||||
</configuration>
|
</configuration>
|
||||||
</plugin>
|
</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>
|
</plugins>
|
||||||
</build>
|
</build>
|
||||||
|
|
||||||
@ -274,6 +289,8 @@
|
|||||||
<!-- Mime Type Libraries -->
|
<!-- Mime Type Libraries -->
|
||||||
<tika.version>1.18</tika.version>
|
<tika.version>1.18</tika.version>
|
||||||
<jmime-magic.version>0.1.5</jmime-magic.version>
|
<jmime-magic.version>0.1.5</jmime-magic.version>
|
||||||
|
<maven-jar-plugin.version>3.1.0</maven-jar-plugin.version>
|
||||||
|
|
||||||
</properties>
|
</properties>
|
||||||
|
|
||||||
</project>
|
</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 {
|
||||||
|
|
||||||
|
try (FileReader fileReader = new FileReader("src/main/resources/input.txt");
|
||||||
|
BufferedReader reader = new BufferedReader(fileReader)) {
|
||||||
|
String contents = reader.lines()
|
||||||
|
.collect(Collectors.joining(System.lineSeparator()));
|
||||||
|
System.out.println(contents);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
private void loadFileAsResource() throws IOException {
|
||||||
|
|
||||||
|
try (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.loadFileAsResource();
|
||||||
|
resourceLoader.loadFileWithReader();
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
Loading…
x
Reference in New Issue
Block a user