Created
March 14, 2012 15:15
-
-
Save milenkovicm/2037175 to your computer and use it in GitHub Desktop.
[Java] - Resource loader
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package com.example.util; | |
import java.io.File; | |
import java.io.InputStream; | |
import org.apache.commons.io.IOUtils; | |
import org.slf4j.Logger; | |
import org.slf4j.LoggerFactory; | |
public final class ResourceReader { | |
private static final Logger LOG = LoggerFactory.getLogger(ResourceReader.class); | |
/** default file encoding */ | |
private static final String FILE_ENCODING = "UTF-8"; | |
private ResourceReader() { | |
} | |
/** | |
* Retrieve InputStream for the file located in resources | |
* @param filename - must be without leading slash | |
* @return InputStream | |
*/ | |
public static InputStream getResourceAsStream(String filename) { | |
LOG.debug("getResourceAsStream() START, filename={}", filename); | |
InputStream inputStream = ResourceReader.class.getClassLoader().getResourceAsStream(filename); | |
if (inputStream == null) { | |
throw new RuntimeException("Can't find resource: " + filename); | |
} | |
LOG.debug("getResourceAsStream() END."); | |
return inputStream; | |
} | |
/** | |
* Retrieve File for the file located in resources | |
* @param filename - must be without leading slash | |
* @return File | |
*/ | |
public static File getResourceAsFile(String filename) { | |
LOG.debug("getResourceAsFile() START, filename={}", filename); | |
if (ResourceReader.class.getClassLoader().getResource(filename) == null) { | |
throw new RuntimeException("Can't find resource: " + filename); | |
} | |
LOG.debug("getResourceAsFile() END, path={}", ResourceReader.class.getClassLoader().getResource(filename).getFile()); | |
return new File(ResourceReader.class.getClassLoader().getResource(filename).getFile()); | |
} | |
/** | |
* Retrieve byte[] for the file located in resources | |
* @param filename - must be without leading slash | |
* @return File | |
*/ | |
public static byte[] getResourceAsByteArray(String filename) { | |
LOG.debug("getResourceAsByteArray() executing for filename={}", filename); | |
try { | |
return IOUtils.toByteArray(ResourceReader.getResourceAsStream(filename)); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
/** | |
* Retrieve String for the file located in resources | |
* @param filename - must be without leading slash | |
* @return File | |
*/ | |
public static String getResourceAsString(String filename) { | |
LOG.debug("getResourceAsString() executing for filename={}", filename); | |
try { | |
return IOUtils.toString((ResourceReader.getResourceAsStream(filename)), FILE_ENCODING ); | |
} catch (Exception e) { | |
throw new RuntimeException(e); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment