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

StringgetFileName(final Object resource)

This method gives the resource file name.

String fileName = null;
if (resource instanceof File) {
    fileName = ((File) resource).getName();
} else {
    fileName = getPath(resource);
    fileName = fileName.substring(fileName.lastIndexOf(File.separator) + 1, fileName.length());
return fileName;
...
FilegetFileResource(String resourceName)
Returns a File from a resource.
try {
    return new File(Resources.getResource(resourceName).toURI());
} catch (final URISyntaxException e) {
    throw new RuntimeException(e);
InputStreamgetInputStream(String resourceOrFile, Class cls)
get Input Stream
try {
    return getURL(resourceOrFile, cls).openStream();
} catch (Exception e) {
    throw new FileNotFoundException(resourceOrFile);
InputStreamgetInputStreamForResource(String s)
Uses getSourceForResource on the passed string and tries to open whatever resource is passed and return the correspondent open input stream.
InputStream ret = null;
Object o = getSourceForResource(s);
if (o != null) {
    if (o instanceof File) {
        try {
            ret = new FileInputStream((File) o);
        } catch (FileNotFoundException e) {
    } else if (o instanceof URL) {
        try {
            ret = ((URL) o).openStream();
        } catch (IOException e) {
return ret;
longgetLastModificationForResource(String resourceId)
Get the last modification date if the resource resolves to a file.
long ret = new Date().getTime();
Object o = getSourceForResource(resourceId);
if (o instanceof File) {
    ret = ((File) o).lastModified();
return ret;
longgetLastModified(final String resourceName)
Use this if last modified in File didnt work , possible in compressed file But it seem that it checks for the modification time for the hall compressed file it worth checking.
try {
    final URL url = Integer.class.getResource(resourceName);
    if (url == null) {
        return -1;
    final URLConnection con = url.openConnection();
    final long lastModified = con.getLastModified();
    con.getInputStream().close();
...
URLgetParentResource(final Class c, final String resource)
get Parent Resource
String name = c.getName();
name = name.substring(0, name.lastIndexOf("."));
name = name.substring(0, name.lastIndexOf(".") + 1);
name = name.replace(".", "/");
return c.getClassLoader().getResource(name + resource);
FilegetSampleDir(File resourcesDir)
get Sample Dir
File file = new File(resourcesDir, "strokes");
if ((!file.exists()) && (!file.mkdirs()))
    throw new RuntimeException("mkdirs failed for " + file.getAbsolutePath());
return file;
StringgetShortName(String resource, Map nsMap)
Gets the short name of a resource
StringBuilder shortName = new StringBuilder();
if (isFullIRI(resource)) {
    String prefix = getPrefix(getNamespace(resource), nsMap);
    if (prefix != null && !prefix.equals("")) {
        shortName.append(getPrefix(getNamespace(resource), nsMap));
        shortName.append(":");
        shortName.append(getElement(resource));
    } else {
...
ObjectgetSourceForResource(String s)
Analyze the passed string and determine if it specifies an existing file resource or URL.
File f = new File(s);
if (f.exists())
    return f;
URL url = null;
try {
    url = new URL(s);
} catch (MalformedURLException e) {
if (url != null)
    return url;
return null;