Loading Resources from classpath in Java with Example

Resources play an important role during runtime by providing necessary files that enable smooth software operation. These resources are accessible through a classpath system which allows them to be read from or written into with ease. For managing this process effectively within Java programs, several APIs such as the ClassLoader and Class classes exist - these offer various features for identifying resource locations within the app's environment and retrieving related information while at it.


  • Step 1 − Get the class loader for the current class

  • Step 2 − Load a resource as a URL/Inputstream.

    For those who choose to employ getResource(), ensure that you are converting your URL effectively through using the getPath() method. For those who rather opt for getResourcesAsStream(), it may be useful in creating either a Reader or an InputStreamReader, allowing for comprehensive examination of all content found within said resource.

  • Step 3 − Read the resource.

  • Step 4 − Close the InputStream, Reader, or InputStreamReader.

  • Step 5 − Handle any exceptions that may occur during the resource loading process

Approach 1: Using the getResourceAsStream() method

An effective method of loading resources involves using getResourceAsStream(). Here's some sample code to illustrate this approach −


import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
public class ResourceLoader {
   public static void main(String[] args) throws IOException {
      ClassLoader classLoader = ResourceLoader.class.getClassLoader();
      InputStream inputStream = classLoader.getResourceAsStream("example.txt");
      byte[] resourceBytes = inputStream.readAllBytes();
      // Close the InputStream
      // Convert the byte array to a String and print it
      String resourceString = new String(resourceBytes, StandardCharsets.UTF_8);


This is an example resource file.


For those wishing to fetch and display text data from a classpath resource called example.txt, this segment of code may be useful. However, do note that making use of getResourceAsStream() will get you an InputStream type return value - this must first be changed into a byte array before being converted later with String(byte[], Charset) constructor.

Approach 2: Using Collections.swap()

This approach involves using getResource() method.The following is a code example −


import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class ResourceLoaderExample {
   public static void main(String[] args) throws IOException {
      ClassLoader classLoader = ResourceLoaderExample.class.getClassLoader();
      URL resourceUrl = classLoader.getResource("example.txt");
      InputStream inputStream = resourceUrl.openStream();
      BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream,
      String line;
      while ((line = reader.readLine()) != null) {


A useful tool for obtaining resources from your classpath is the getResource() method. By calling this function. You can obtain a resource URL and sequentially read its contents using an InputStream and BufferedReader. Be mindful that when providing a path argument for this approach it must be relative in orientation to your classpaths' root - much like with utilizing getResourceAsStream().

Comparison Between Approaches


Approach 1

Approach 2


Loading the file as inputstream

Loading the file as url




Method logic

getResourceAsStream() returns an InputStream directly.

getResource() returns a URL object that can be used to obtain an InputStream or a URLConnection.


In summary, there exist diverse methods of loading resources from classpath in Java-such as utilizing ClassLoader.getResource() or ClassLoader.getResourceAsStream(). Subsequently, determining which mode to apply is contingent on individual usage criteria and respective requisites of your app. Once you have secured an InputStream or URL towards the resource file- you can apply varying tactics when reading its contents; using a BufferedReader serves for text files. Loading resources from the classpath is a common task in Java, and understanding how to do it properly is important for developing robust and efficient Java applications.

Updated on: 28-Jul-2023


Kickstart Your Career

Get certified by completing the course

Get Started