Java Resource File getInputStream(String resourceOrFile, Class cls)

Here you can find the source of getInputStream(String resourceOrFile, Class cls)

Description

get Input Stream

License

Apache License

Declaration

public static InputStream getInputStream(String resourceOrFile, Class<?> cls) throws FileNotFoundException 

Method Source Code


//package com.java2s;
//License from project: Apache License 

import java.io.File;
import java.io.FileNotFoundException;

import java.io.InputStream;

import java.net.MalformedURLException;

import java.net.URL;

public class Main {
    public static InputStream getInputStream(String resourceOrFile) throws FileNotFoundException {
        return getInputStream(resourceOrFile, Thread.class);
    }/*from   w ww .ja va  2 s  . c om*/

    public static InputStream getInputStream(String resourceOrFile, Class<?> cls) throws FileNotFoundException {
        try {
            return getURL(resourceOrFile, cls).openStream();
        } catch (Exception e) {
            throw new FileNotFoundException(resourceOrFile);
        }
    }

    public static URL getURL(String resourceOrFile, Class<?> cls) throws FileNotFoundException {

        File file = new File(resourceOrFile);
        // System.out.println("checking file ");
        // is file
        if (file.exists()) {
            // System.out.println("file exists");
            try {
                return file.toURI().toURL();
            } catch (MalformedURLException e) {
                throw new FileNotFoundException(resourceOrFile);
            }
        }
        // is resource
        if (!file.exists()) {
            // System.out.println("file resource");
            URL url = cls.getResource(resourceOrFile);
            if (url != null) {
                return url;
            }
            url = cls.getResource("/" + resourceOrFile);
            if (url != null) {
                return url;
            }
        }
        throw new FileNotFoundException(resourceOrFile);
    }
}

Related

  1. getFileFromRelativeResource(final Class type, final String relativeResourceName)
  2. getFileFromSystemResources(String fileName)
  3. getFilename(Class clazz, String resource)
  4. getFileName(final Object resource)
  5. getFileResource(String resourceName)
  6. getInputStreamForResource(String s)
  7. getLastModificationForResource(String resourceId)
  8. getLastModified(final String resourceName)
  9. getParentResource(final Class c, final String resource)