Skip to content

Instantly share code, notes, and snippets.

@alexandreaquiles
Last active April 3, 2022 13:43
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save alexandreaquiles/505d414da0ce91f555cb4be86121194b to your computer and use it in GitHub Desktop.
Save alexandreaquiles/505d414da0ce91f555cb4be86121194b to your computer and use it in GitHub Desktop.
package br.com.paradizo.tema;
import java.io.IOException;
import java.net.URI;
import java.net.URISyntaxException;
import java.nio.file.*;
import java.util.Collections;
public class FileUtils {
public static String getResourceContents(String resource) {
try {
Path resourcePath = getResourceAsPath(resource);
return getPathContents(resourcePath);
} catch (URISyntaxException | IOException ex) {
throw new IllegalStateException(ex);
}
}
private static Path getResourceAsPath(String resource) throws URISyntaxException, IOException {
URI uri = FileUtils.class.getResource(resource).toURI();
if (isResourceInJar(uri)) {
return getResourceFromJar(uri);
} else {
return Paths.get(uri);
}
}
private static boolean isResourceInJar(URI uri) {
return uri.getScheme().equals("jar");
}
private static Path getResourceFromJar(URI fullURI) throws IOException {
// a resource from a JAR needs a FileSystem
// see http://stackoverflow.com/a/22605905
String[] uriParts = fullURI.toString().split("!");
URI jarURI = URI.create(uriParts[0]);
FileSystem fs;
try {
fs = FileSystems.newFileSystem(jarURI, Collections.<String, String>emptyMap());
} catch (FileSystemAlreadyExistsException ex) {
fs = FileSystems.getFileSystem(jarURI);
}
String resourceURI = uriParts[1];
return fs.getPath(resourceURI);
}
private static String getPathContents(Path path) throws IOException {
return Files.readString(path);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment