|
6 | 6 |
|
7 | 7 | import java.io.BufferedInputStream; |
8 | 8 | import java.io.File; |
9 | | -import java.io.FileInputStream; |
10 | 9 | import java.io.FileNotFoundException; |
11 | 10 | import java.io.IOException; |
12 | 11 | import java.io.InputStream; |
13 | 12 | import java.net.URI; |
14 | 13 | import java.net.URL; |
15 | 14 | import java.nio.charset.StandardCharsets; |
| 15 | +import java.nio.file.Files; |
16 | 16 | import java.util.Scanner; |
17 | 17 | import lombok.EqualsAndHashCode; |
18 | 18 |
|
@@ -56,10 +56,10 @@ private TextResource(final String text) { |
56 | 56 | * @param file File to represent as text. |
57 | 57 | * @throws FileNotFoundException If file not found |
58 | 58 | */ |
59 | | - TextResource(final File file) throws FileNotFoundException { |
| 59 | + TextResource(final File file) throws IOException { |
60 | 60 | this( |
61 | 61 | TextResource.readAsString( |
62 | | - new BufferedInputStream(new FileInputStream(file)) |
| 62 | + new BufferedInputStream(Files.newInputStream(file.toPath())) |
63 | 63 | ) |
64 | 64 | ); |
65 | 65 | } |
@@ -93,18 +93,15 @@ public String toString() { |
93 | 93 | * @return The stream content, in String form |
94 | 94 | */ |
95 | 95 | private static String readAsString(final InputStream stream) { |
96 | | - final Scanner scanner = new Scanner( |
97 | | - stream, StandardCharsets.UTF_8.name() |
98 | | - ).useDelimiter("\\A"); |
99 | 96 | final String result; |
100 | | - try { |
| 97 | + try (Scanner scanner = new Scanner( |
| 98 | + stream, StandardCharsets.UTF_8.name() |
| 99 | + ).useDelimiter("\\A")) { |
101 | 100 | if (scanner.hasNext()) { |
102 | 101 | result = scanner.next(); |
103 | 102 | } else { |
104 | 103 | result = ""; |
105 | 104 | } |
106 | | - } finally { |
107 | | - scanner.close(); |
108 | 105 | } |
109 | 106 | return result; |
110 | 107 | } |
|
0 commit comments