https://github.com/kherge/java.resource
A library to simplify resource access.
https://github.com/kherge/java.resource
jar java resources
Last synced: 11 months ago
JSON representation
A library to simplify resource access.
- Host: GitHub
- URL: https://github.com/kherge/java.resource
- Owner: kherge
- Created: 2019-05-31T03:56:08.000Z (about 7 years ago)
- Default Branch: master
- Last Pushed: 2019-06-01T03:20:50.000Z (about 7 years ago)
- Last Synced: 2024-10-27T15:33:37.015Z (over 1 year ago)
- Topics: jar, java, resources
- Language: Java
- Size: 85 KB
- Stars: 5
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Contributing: CONTRIBUTING.md
Awesome Lists containing this project
README
[](https://travis-ci.org/kherge/java.resource)
[](https://sonarcloud.io/dashboard?id=kherge_java.resource)
Resource
========
A library to simplify resource access.
Resource access behaves differently depending on context. For example, resources can be on the file
system (during testing) or contained in a JAR (after a build). This library provides a more
consistent method of accessing resource by eliminating this distinction.
```java
Resource resource = new Resource(MyClass.class.getClassLoader());
System.out.println(resource.getAsString("hello-world.txt", "utf-8"));
System.out.println(resource.list("path/to/folder"));
```
Requirements
------------
- Java 8
Installation
------------
### Gradle
```groovy
compile 'io.herrera.kevin:resource:1.1.0'
```
### Maven
```xml
io.herrera.kevin
resource
1.1.0
```
Usage
-----
```java
import io.herrera.kevin.resource.Resource;
import java.io.InputStream;
import java.nio.file.Path;
class Example {
public static void main() {
try {
// The manager is created using the desired class loader.
Resource resource = new Resource(Example.class.getClassLoader());
// A resource can be retrieved as an InputStream.
InputStream stream = resource.getAsInputStream("my-resource.txt");
// A resource can be retrieved as a file path.
Path path = resource.getAsPath("my-resource.txt");
// A resource can be retrieved as a string.
String string = resource.getAsString("my-resource.txt");
// List resources in a folder.
List resources = resource.list("path/to/folder");
// List matching resources in a folder.
// (This is more performant than using list() and then filtering.)
List resources = resource.listMatch("path/to/folder", ".+pattern.+");
// Stream resources in a folder.
try (Stream resources = resource.stream("path/to/folder")) {
resources.forEach(name -> System.out.println(resource.getAsString(name));
}
} catch (ResourceException exception) {
// If a resource could not be retrieved or listed, this exception is thrown.
throw exception;
}
}
}
```
License
-------
This library is made available under the MIT and Apache 2.0 licenses.