Skip to content

Commit 086e5ca

Browse files
committed
Add FileUtility#getResourceContent(String)
1 parent 4f2f207 commit 086e5ca

File tree

1 file changed

+25
-3
lines changed

1 file changed

+25
-3
lines changed

src/main/java/xyz/srnyx/javautilities/FileUtility.java

Lines changed: 25 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,10 @@
44

55
import java.io.File;
66
import java.io.IOException;
7+
import java.io.InputStream;
78
import java.nio.file.Files;
89
import java.nio.file.Path;
9-
import java.util.Arrays;
10-
import java.util.Collections;
11-
import java.util.Set;
10+
import java.util.*;
1211
import java.util.stream.Collectors;
1312

1413

@@ -55,6 +54,29 @@ public static Set<String> getFileNames(@NotNull File file, @NotNull String exten
5554
.collect(Collectors.toSet());
5655
}
5756

57+
/**
58+
* Get the content of a resource file as a {@link List} of {@link String}s
59+
*
60+
* @param fileName the name of the resource file
61+
*
62+
* @return the content of the resource file
63+
*/
64+
@NotNull
65+
public static List<String> getResourceContent(@NotNull String fileName) {
66+
try (final InputStream inputStream = FileUtility.class.getResourceAsStream(fileName)) {
67+
if (inputStream == null) throw new NullPointerException("Failed to get resource: " + fileName);
68+
final List<String> content = new ArrayList<>();
69+
try (final Scanner scanner = new Scanner(inputStream)) {
70+
while (scanner.hasNextLine()) content.add(scanner.nextLine());
71+
} catch (final NullPointerException e) {
72+
e.printStackTrace();
73+
}
74+
return content;
75+
} catch (final IOException e) {
76+
throw new RuntimeException("Failed to read resource: " + fileName, e);
77+
}
78+
}
79+
5880
/**
5981
* Constructs a new {@link FileUtility} instance (illegal)
6082
*

0 commit comments

Comments
 (0)