Partner – Moderne – NPI EA (cat=Spring Boot)
announcement - icon

Refactor Java code safely — and automatically — with OpenRewrite.

Refactoring big codebases by hand is slow, risky, and easy to put off. That’s where OpenRewrite comes in. The open-source framework for large-scale, automated code transformations helps teams modernize safely and consistently.

Each month, the creators and maintainers of OpenRewrite at Moderne run live, hands-on training sessions — one for newcomers and one for experienced users. You’ll see how recipes work, how to apply them across projects, and how to modernize code with confidence.

Join the next session, bring your questions, and learn how to automate the kind of work that usually eats your sprint time.

Course – Black Friday 2026 – NPI EA – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – before_post_content
announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

1. Introduction

In this tutorial, we’ll demonstrate various ways to access and load the contents of a file that’s on the classpath using Spring.

Further reading:

A Guide to the ResourceBundle

It's always challenging to maintain and extend multilingual applications. This article covers how to use the ResourceBundle to cope with the varieties that come up when you need to provide the same content to different cultures.

Load a Resource as a String in Spring

Learn how to inject the contents of a resource file into our beans as a String, with Spring's Resource class making this very easy.

2. Using Resource

The Resource interface helps in abstracting access to low-level resources. In fact, it supports the handling of all kinds of file resources in a uniform manner.

Let’s start by looking at various methods to obtain a Resource instance.

2.1. Manually

For accessing a resource from the classpath, we can simply use ClassPathResource:

public Resource loadEmployeesWithClassPathResource() {
    return new ClassPathResource("data/employees.dat");
}

By default, ClassPathResource removes some boilerplate by selecting between the thread’s context classloader and the default system classloader.

However, we can also indicate the classloader to use either directly:

return new ClassPathResource("data/employees.dat", this.getClass().getClassLoader());

Or indirectly through a specified class:

return new ClassPathResource(
  "data/employees.dat", 
  Employee.class.getClassLoader());

Note that from Resource, we can easily jump to Java standard representations like InputStream or File.

Another thing to note here is that the above method works only for absolute paths. If we want to specify a relative path, we can pass a second class argument. The path will be relative to this class:

new ClassPathResource("../../../data/employees.dat", Example.class).getFile();

The file path above is relative to the Example class.

2.2. Using @Value

We can also inject a Resource with @Value:

@Value("classpath:data/resource-data.txt")
Resource resourceFile;

@Value supports other prefixes too, like file: and url:.

2.3. Using ResourceLoader

If we want to lazily load our resource, we can use ResourceLoader:

@Autowired
ResourceLoader resourceLoader;

Then we retrieve our resource with getResource:

public Resource loadEmployeesWithResourceLoader() {
    return resourceLoader.getResource(
      "classpath:data/employees.dat");
}

Note too that ResourceLoader is implemented by all concrete ApplicationContexts, which means that we can also simply depend on ApplicationContext if that suits our situation better:

ApplicationContext context;

public Resource loadEmployeesWithApplicationContext() {
    return context.getResource("classpath:data/employees.dat");
}

3. Using ResourceUtils

As a caveat, there is another way to retrieve resources in Spring, but the ResourceUtils Javadoc is clear that the class is mainly for internal use.

If we see usages of ResourceUtils in our code:

public File loadEmployeesWithSpringInternalClass() 
  throws FileNotFoundException {
    return ResourceUtils.getFile(
      "classpath:data/employees.dat");
}

We should carefully consider the rationale, as it’s probably better to use one of the standard approaches above.

4. Reading Resource Data

Once we have a Resource, it’s easy for us to read the contents. As we have already discussed, we can easily obtain a File or an InputStream reference from the Resource.

Let’s imagine we have the following file, data/employees.dat, on the classpath:

Joe Employee,Jan Employee,James T. Employee

4.1. Reading as a File

Now we can read its contents by calling getFile:

@Test
public void whenResourceAsFile_thenReadSuccessful() 
  throws IOException {
 
    File resource = new ClassPathResource(
      "data/employees.dat").getFile();
    String employees = new String(
      Files.readAllBytes(resource.toPath()));
    assertEquals(
      "Joe Employee,Jan Employee,James T. Employee", 
      employees);
}

Although, it should be noted that this approach expects the resource to be present in the filesystem and not within a jar file.

4.2. Reading as an InputStream

Let’s say though, that our resource is inside a jar.

Then we can instead read a Resource as an InputStream:

@Test
public void whenResourceAsStream_thenReadSuccessful() 
  throws IOException {
    InputStream resource = new ClassPathResource(
      "data/employees.dat").getInputStream();
    try ( BufferedReader reader = new BufferedReader(
      new InputStreamReader(resource)) ) {
        String employees = reader.lines()
          .collect(Collectors.joining("\n"));
 
        assertEquals("Joe Employee,Jan Employee,James T. Employee", employees);
    }
}

5. Conclusion

In this brief article, we’ve examined a few ways to access and read a resource from the classpath using Spring. This includes eager and lazy loading, and on the filesystem or in a jar.

The code backing this article is available on GitHub. Once you're logged in as a Baeldung Pro Member, start learning and coding on the project.
Partner – Moderne – NPI EA (tag=Refactoring)
announcement - icon

Modern Java teams move fast — but codebases don’t always keep up. Frameworks change, dependencies drift, and tech debt builds until it starts to drag on delivery. OpenRewrite was built to fix that: an open-source refactoring engine that automates repetitive code changes while keeping developer intent intact.

The monthly training series, led by the creators and maintainers of OpenRewrite at Moderne, walks through real-world migrations and modernization patterns. Whether you’re new to recipes or ready to write your own, you’ll learn practical ways to refactor safely and at scale.

If you’ve ever wished refactoring felt as natural — and as fast — as writing code, this is a good place to start.

Course – Black Friday 2026 – NPI EA – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW

Course – Black Friday 2026 – NPI – main – after_post_content

announcement - icon

Yes, we're now running our Black Friday Sale. All Courses are 33% off until 1st December, 2026 at 17:00 Romania time:

>> EXPLORE ACCESS NOW