Java Utililty Methods Resource File

List of utility methods to do Resource File

Description

The list of methods to do Resource File are organized into topic(s).

Method

InputStreamgetStreamFromResource(Class clazz, String resourceName)
Get a string from a Resource .
if (resourceName.startsWith("res://") && clazz != null) {
    URL url = clazz.getResource(resourceName.substring(6));
    if (url == null) {
        throw new FileNotFoundException("Resource " + resourceName + " not found on CLASSPATH.");
    return url.openStream();
} else {
    try {
...
FilegetTestResourceFile(String pName, Class pClass)
get Test Resource File
URL lFile = pClass.getResource(pName);
return new File(lFile.getFile());
InputStreaminputStream(Class baseClass, String resourceName)
input Stream
URL url = baseClass.getResource(resourceName);
if (url == null) {
    throw new FileNotFoundException(String.format("Resource file is not found. %s", resourceName));
return url.openStream();
booleanisFullIRI(String resource)
Determines if the resource is a full iri
try {
    new URL(resource);
    return true;
} catch (MalformedURLException ex) {
    return false;
ListloadAutoConfigFiles(ClassLoader classLoader, String resourceName)
load Auto Config Files
try {
    List<URL> urlList = new ArrayList<URL>();
    Enumeration<URL> urls = classLoader.getResources(resourceName);
    for (; urls.hasMoreElements();) {
        URL url = urls.nextElement();
        urlList.add(url);
    return urlList;
...
InputStreamloadConfigFile(String resource, Class clazz)
load Config File
ClassLoader classLoader = null;
try {
    Method method = Thread.class.getMethod("getContextClassLoader");
    classLoader = (ClassLoader) method.invoke(Thread.currentThread());
} catch (Exception e) {
    System.out.println("loadConfigFile error: ");
    e.printStackTrace();
if (classLoader == null) {
    classLoader = clazz.getClassLoader();
try {
    if (classLoader != null) {
        URL url = classLoader.getResource(resource);
        if (url == null) {
            System.out.println("Can not find resource:" + resource);
            return null;
        if (url.toString().startsWith("jar:file:")) {
            System.out.println("Get resource \"" + resource + "\" from jar:\t" + url.toString());
            return clazz.getResourceAsStream(resource.startsWith("/") ? resource : "/" + resource);
        } else {
            System.out.println("Get resource \"" + resource + "\" from:\t" + url.toString());
            return new FileInputStream(new File(url.toURI()));
} catch (Exception e) {
    System.out.println("loadConfigFile error: ");
    e.printStackTrace();
return null;
StringloadUTF8(String resource)
load UTF
File source = getFile(resource);
if (source == null) {
    return null;
InputStream in = new FileInputStream(source);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
pipe(in, bos);
in.close();
...
InputStreamopenInputStream(String resourceString, ClassLoader classLoader)
open Input Stream
int indexOfColon = resourceString.indexOf(':');
String prefix = resourceString.substring(0, indexOfColon);
if ("classpath".equalsIgnoreCase(prefix)) {
    String path = resourceString.substring(indexOfColon + 1);
    return classLoader.getResourceAsStream(path);
return new URL(resourceString).openStream();
StringparseASX(String inputResource)
parse ASX
String inputFile = "";
try {
    String contents = readTextFromUrl(new URL(inputResource));
    for (String line : contents.split("\n")) {
        if (line.toLowerCase().contains("href")) {
            String pattern = "(?i).*href=\"(.*)\".*";
            inputFile = line.replaceAll(pattern, "$1");
            break;
...
voidprintUsage(ResourceBundle bundle)
Prints the usage through system out.
System.out.println(bundle.getString("message.info.usage"));