Skip to content

Instantly share code, notes, and snippets.

@gkhays
Last active February 13, 2020 00:42
Show Gist options
  • Save gkhays/fc150cf457e77b245a1a40fb0be07710 to your computer and use it in GitHub Desktop.
Save gkhays/fc150cf457e77b245a1a40fb0be07710 to your computer and use it in GitHub Desktop.
Retrieve a resource on the classpath

Get Resource

Read a file in from a resource. In many cases it is useful to read it into a string. Use a standard Maven directory layout.

├───src
│   ├───main
│   │   ├───java
│   │   └───resources
│   └───test
│       ├───java
│       └───resources
URL url = getClass().getClassLoader().getResource("test.txt");
Path path = Paths.get(url.toURI());

String contents = new String(Files.readAllBytes(path));
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment