Skip to content

Instantly share code, notes, and snippets.

@bcalmac
Created February 10, 2020 16:06
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 bcalmac/e78a73f887102f7592e463d08c2ce2a3 to your computer and use it in GitHub Desktop.
Save bcalmac/e78a73f887102f7592e463d08c2ce2a3 to your computer and use it in GitHub Desktop.
Load resource to string
package io.github.bcalmac.overtime.server.utils;
import java.io.IOException;
import java.net.URISyntaxException;
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Paths;
public class OvertimeTestUtils {
public static String loadResource(String resource) {
URL resourceURL = OvertimeTestUtils.class.getResource(resource);
if (resourceURL == null) {
throw new RuntimeException("Missing resource: " + resource);
}
try {
return Files.readString(Paths.get(resourceURL.toURI()));
} catch (IOException | URISyntaxException e) {
throw new RuntimeException("Failed to load resource: " + resource, e);
}
}
}
@bcalmac
Copy link
Author

bcalmac commented Feb 10, 2020

Requires Java 11 for Files.readString().

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment