|
| 1 | +package com.mkyong.io.howto.resources; |
| 2 | + |
| 3 | +import java.io.*; |
| 4 | +import java.net.URI; |
| 5 | +import java.net.URISyntaxException; |
| 6 | +import java.net.URL; |
| 7 | +import java.nio.charset.StandardCharsets; |
| 8 | +import java.nio.file.FileSystem; |
| 9 | +import java.nio.file.*; |
| 10 | +import java.util.Collections; |
| 11 | +import java.util.List; |
| 12 | +import java.util.stream.Collectors; |
| 13 | + |
| 14 | +public class FileResourcesUtils { |
| 15 | + |
| 16 | + public static void main(String[] args) throws IOException { |
| 17 | + |
| 18 | + //String fileName = "database.properties"; |
| 19 | + String fileName = "json/file1.json"; |
| 20 | + |
| 21 | + FileResourcesUtils app = new FileResourcesUtils(); |
| 22 | + |
| 23 | + System.out.println("getResourceAsStream : " + fileName); |
| 24 | + InputStream is = app.getFileFromResourceAsStream(fileName); |
| 25 | + printInputStream(is); |
| 26 | + |
| 27 | + System.out.println("\ngetResource : " + fileName); |
| 28 | + File file = app.getFileFromResource(fileName); |
| 29 | + printFile(file); |
| 30 | + |
| 31 | + // get all files from a resources folder (JAR version) |
| 32 | + /*try { |
| 33 | +
|
| 34 | + List<Path> result = app.getPathsFromResourceJAR(); |
| 35 | + for (Path path : result) { |
| 36 | + System.out.println("Path : " + path); |
| 37 | + InputStream is = app.getFileFromResourceAsStream(path.toString()); |
| 38 | + printInputStream(is); |
| 39 | + } |
| 40 | +
|
| 41 | + } catch (URISyntaxException e) { |
| 42 | + e.printStackTrace(); |
| 43 | + }*/ |
| 44 | + |
| 45 | + } |
| 46 | + |
| 47 | + |
| 48 | + // get a file from resources folder |
| 49 | + // works in everywhere, IDEA, unit test and JAR file. |
| 50 | + private InputStream getFileFromResourceAsStream(String fileName) { |
| 51 | + |
| 52 | + // The class loader that loaded the class |
| 53 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 54 | + |
| 55 | + InputStream inputStream = classLoader.getResourceAsStream(fileName); |
| 56 | + |
| 57 | + if (inputStream == null) { |
| 58 | + throw new IllegalArgumentException("file not found! " + fileName); |
| 59 | + } else { |
| 60 | + return inputStream; |
| 61 | + } |
| 62 | + |
| 63 | + } |
| 64 | + |
| 65 | + // The a resource URL is not working in JAR file |
| 66 | + // Exception in thread "main" java.nio.file.NoSuchFileException: |
| 67 | + // file:/home/mkyong/projects/core-java/java-io/target/java-io.jar!/json/file1.json |
| 68 | + private File getFileFromResource(String fileName) { |
| 69 | + |
| 70 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 71 | + |
| 72 | + URL resource = classLoader.getResource(fileName); |
| 73 | + if (resource == null) { |
| 74 | + throw new IllegalArgumentException("file not found! " + fileName); |
| 75 | + } else { |
| 76 | + return new File(resource.getFile()); |
| 77 | + } |
| 78 | + |
| 79 | + } |
| 80 | + |
| 81 | + // Get all path from the JAR file |
| 82 | + private List<Path> getPathsFromResourceJAR() throws URISyntaxException, IOException { |
| 83 | + |
| 84 | + List<Path> result; |
| 85 | + |
| 86 | + // get path of the running JAR |
| 87 | + String jarPath = getClass().getProtectionDomain().getCodeSource().getLocation().toURI().getPath(); |
| 88 | + System.out.println("path :" + jarPath); |
| 89 | + |
| 90 | + URI uri = URI.create("jar:file:" + jarPath); |
| 91 | + |
| 92 | + try (FileSystem fs = FileSystems.newFileSystem(uri, Collections.emptyMap())) { |
| 93 | + |
| 94 | + // get paths from src/main/resources/json |
| 95 | + result = Files.walk(fs.getPath("json")) |
| 96 | + .filter(Files::isRegularFile) |
| 97 | + .collect(Collectors.toList()); |
| 98 | + } |
| 99 | + |
| 100 | + return result; |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | + // Works in IDE and unit test, not working in JAR |
| 105 | + private List<File> getAllFilesFromResource() throws URISyntaxException, IOException { |
| 106 | + |
| 107 | + ClassLoader classLoader = getClass().getClassLoader(); |
| 108 | + |
| 109 | + // src/main/resources/json |
| 110 | + URL resource = classLoader.getResource("json"); |
| 111 | + |
| 112 | + List<File> collect = Files.walk(Paths.get(resource.toURI())) |
| 113 | + .filter(Files::isRegularFile) |
| 114 | + .map(x -> x.toFile()) |
| 115 | + .collect(Collectors.toList()); |
| 116 | + |
| 117 | + return collect; |
| 118 | + } |
| 119 | + |
| 120 | + // print input stream |
| 121 | + private static void printInputStream(InputStream inputStream) throws IOException { |
| 122 | + |
| 123 | + try (InputStreamReader streamReader = |
| 124 | + new InputStreamReader(inputStream, StandardCharsets.UTF_8); |
| 125 | + BufferedReader reader = new BufferedReader(streamReader)) { |
| 126 | + |
| 127 | + String line; |
| 128 | + while ((line = reader.readLine()) != null) { |
| 129 | + System.out.println(line); |
| 130 | + } |
| 131 | + |
| 132 | + } |
| 133 | + |
| 134 | + } |
| 135 | + |
| 136 | + // print a file |
| 137 | + private static void printFile(File file) { |
| 138 | + List<String> lines; |
| 139 | + try { |
| 140 | + lines = Files.readAllLines(file.toPath(), StandardCharsets.UTF_8); |
| 141 | + lines.forEach(System.out::println); |
| 142 | + } catch (IOException e) { |
| 143 | + e.printStackTrace(); |
| 144 | + } |
| 145 | + |
| 146 | + } |
| 147 | + |
| 148 | +} |
0 commit comments