Class: getResourceAsStream(String name) : Class « java.lang « Java by API






Class: getResourceAsStream(String name)

   
import java.awt.Image;
import java.awt.Toolkit;
import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;

public final class MainClass {

  private MainClass() {
  }

  public static Image getImage(Class relativeClass, String filename) {
    Image returnValue = null;
    InputStream is = relativeClass.getResourceAsStream(filename);
    if (is != null) {
      BufferedInputStream bis = new BufferedInputStream(is);
      ByteArrayOutputStream baos = new ByteArrayOutputStream();
      try {
        int ch;
        while ((ch = bis.read()) != -1) {
          baos.write(ch);
        }
        returnValue = Toolkit.getDefaultToolkit().createImage(
            baos.toByteArray());
      } catch (IOException exception) {
        System.err.println("Error loading: " + filename);
      }
    }
    return returnValue;
  }
}


           
         
    
    
  








Related examples in the same category

1.Class: forName(String classDec)
2.Class: Annotation[] getAnnotations()
3.Class: getCanonicalName()
4.Class: getClasses()
5.Class: getComponentType()
6.Class: getConstructors()
7.Class: getConstructor(Class...)
8.Class: getDeclaringClass()
9.Class: getDeclaredConstructors()
10.Class: getDeclaredFields()
11.Class: getDeclaredMethods()
12.Class: getDeclaredMethod(String name, Class... parameterTypes)
13.Class: getEnumConstants()
14.Class: getFields()
15.Class: getGenericInterfaces()
16.Class: getGenericSuperclass()
17.Class: getInterfaces()
18.Class: Method getMethod(String name, Class... parameterTypes)
19.Class: getMethods()
20.Class: getModifiers()
21.Class: getName()
22.Class: getPackage()
23.Class: getProtectionDomain()
24.Class: getResource(String name) (absolute from the classpath)
25.Class: getResource(String name) (relative to the class location)
26.Class: getSuperclass()
27.Class: getTypeParameters()
28.Class: isArray()
29.Class: isAssignableFrom(Class cls)
30.Class: isEnum()
31.Class: isInstance(Object obj)
32.Class: isInterface()
33.Class: newInstance()