Committing review changes

- change to try-with-resources
- change the order of main method
This commit is contained in:
Alex Tighe 2019-08-05 22:53:17 -04:00
parent 88cd75e519
commit 65e0cdd9dd
3 changed files with 132 additions and 144 deletions

View File

@ -1,6 +0,0 @@
=========
## Core Java IO Cookbooks and Examples
### Relevant Articles:
- [Reading Files Versus Loading Resources](http://www.baeldung.com/reading-files-versus-loading-resources)

View File

@ -48,10 +48,6 @@
</dependencies> </dependencies>
<build> <build>
<finalName>core-java-io2</finalName> <finalName>core-java-io2</finalName>
<resources> <resources>

View File

@ -11,31 +11,29 @@ public class MyResourceLoader {
private void loadFileWithReader() throws IOException { private void loadFileWithReader() throws IOException {
FileReader fileReader = new FileReader("src/main/resources/input.txt"); try (FileReader fileReader = new FileReader("src/main/resources/input.txt"); BufferedReader reader = new BufferedReader(fileReader)) {
BufferedReader reader = new BufferedReader(fileReader);
String contents = reader.lines() String contents = reader.lines()
.collect(Collectors.joining(System.lineSeparator())); .collect(Collectors.joining(System.lineSeparator()));
reader.close();
System.out.println(contents); System.out.println(contents);
}
} }
private void loadFileAsResource() throws IOException { private void loadFileAsResource() throws IOException {
InputStream inputStream = getClass().getResourceAsStream("/input.txt"); try (InputStream inputStream = getClass().getResourceAsStream("/input.txt"); BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream))) {
BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
String contents = reader.lines() String contents = reader.lines()
.collect(Collectors.joining(System.lineSeparator())); .collect(Collectors.joining(System.lineSeparator()));
System.out.println(contents); System.out.println(contents);
}
} }
public static void main(String[] args) throws IOException { public static void main(String[] args) throws IOException {
MyResourceLoader resourceLoader = new MyResourceLoader(); MyResourceLoader resourceLoader = new MyResourceLoader();
resourceLoader.loadFileWithReader();
resourceLoader.loadFileAsResource(); resourceLoader.loadFileAsResource();
resourceLoader.loadFileWithReader();
} }