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

voidcopyDecompressed(String resource, File output)
copy Decompressed
URL input = Resources.getResource(resource);
ByteStreams.copy(new GZIPInputStream(input.openStream()), new FileOutputStream(output));
FilecopyResource(String name)
copy Resource
InputStream is = URLClassLoader.getSystemResourceAsStream(name);
String tempDir = System.getProperty("java.io.tmpdir");
File tmp = new File(tempDir + File.separator + name);
tmp.deleteOnExit();
ByteStreams.copy(is, new FileOutputStream(tmp));
return tmp;
StringexpendResource(String fileName)
expend Resource
URL url = Thread.currentThread().getContextClassLoader().getResource(fileName);
if (url != null) {
    return new File(url.getPath()).toString();
} else {
    throw new IOException(String.format("Resource file [%s] doesn't exist", fileName));
URL[]getAllResources(Class clazz, String resource)
get All Resources
if (resource == null || resource.trim().length() == 0) {
    throw new IllegalArgumentException("resource is null!");
ArrayList<URL> urlList = new ArrayList<URL>();
if (clazz != null) {
    URL url = clazz.getResource(resource);
    if (url != null) {
        urlList.add(url);
...
StringgetBaseLineFolder(String resourceFile)
get Base Line Folder
String result = null;
try {
    ClassLoader classLoader = Thread.currentThread().getContextClassLoader();
    URL localUrl = classLoader.getResource(resourceFile);
    if (localUrl != null) {
        result = convertFileUrlToPath(localUrl);
} catch (Exception ex) {
...
Map>getBundlesContainingResource(BundleContext bundleContext, String resourcePattern)
Returns all bundles that contain a class
Map<Bundle, List<String>> result = new HashMap<Bundle, List<String>>();
Bundle[] bundles = bundleContext.getBundles();
for (Bundle bundle : bundles) {
    List<String> entries = findEntries(bundle, resourcePattern);
    if (entries != null && entries.size() != 0) {
        result.put(bundle, entries);
return result;
byte[]getBytes(Class contextClass, String resourceName)
get Bytes
URL resourceUrl = Resources.getResource(contextClass, resourceName);
return getBytes(resourceUrl);
URLgetClassResource(Class clazz)
get Class Resource
String simpleName = clazz.getSimpleName();
return clazz.getResource(simpleName + ".class");
StringgetContentAsString(String resource)
get Content As String
InputStreamReader isr = getInputStreamReader(resource);
return getContentAsString(isr);
URLgetDefaultWebXmlResourceLocation()
get Default Web Xml Resource Location
return Thread.currentThread().getContextClassLoader()
        .getResource("org/bladerunnerjs/model/appserver/non-j2ee-app-web.xml");