• The resource in the jar file must be qualified with same package name as the class you call getResourceAsStream from.
  • The following examples show how to use javax.servlet.ServletContext#getResourceAsStream() .
  • 2. Class. getClassLoader. getResourceAsStream (String path): It is obtained from the root of the ClassPath by default.
  • getResourceAsStream() locates and loads a file/resource using classloader of classname i.e using the ClassLoader of the class it is called on.
  • The classLoader's getResourceAsStream() method is actually intended to be used for relative paths (otherwise you would just use a FileInputStream).
  • getResourceAsStream(java.lang.ClassLoader loader, java.lang.String resource) Returns a resource on the classpath as a Stream object.
  • There are two ways to load resources in Java, one by using * getResourceAsStream() and getResource() method from java.lang.Class.
  • Config.class.getClassLoader().getResourceAsStream("MyTest.txt"); byte [] b = new byte[256]; int val = 0; String txt = null; do {.
  • getResourceAsStream. January 19, 2012 Leave a comment.
  • Do you need to close getResourceAsStream? getResourceAsStream() will always return a stream that points to a file inside your class’ JAR file.