Read file from resources folder in Spring Boot
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
In Spring Boot applications, reading files from various sources is a common requirement. This article will focus on reading files from the resources folder, which is a standard directory structure used for storing application resources such as configuration files, static assets, and templates.
Understanding the Resources Folder
The resources folder in a Spring Boot application is typically located inside the src/main/resources directory. When the application is packaged, these resources are included in the classpath, and you can easily access them using various utilities provided by Spring Framework.
Accessing Files from the Resources Folder
Spring Boot provides several ways to read files from the resources folder. Here we'll discuss three primary approaches:
- Using
ResourceLoader - Using
@Valueannotation withResourceLoaderAware - Using Spring's
ResourceUtils
Using ResourceLoader
ResourceLoader is an interface provided by Spring that allows you to obtain a Resource from different sources. Here is an example of how to use ResourceLoader to read a file:
Using @Value Annotation
Another common way to read a file is by using the @Value annotation in combination with ResourceLoaderAware. Here's how you can do it:
Using Spring's ResourceUtils
ResourceUtils is a utility class in Spring that can be used to get a URL or File reference to resources. Here's an example:
Configuration Files
In some cases, you may need to define file paths in application properties for dynamic loading. You can use the following configuration:
And access it using:
Handling Exceptions
When reading files, it's important to handle exceptions properly, such as IOException for input/output errors and potentially missing resources.
Summary Table
| Approach | Description | Use Case |
| ResourceLoader | Loads resources using Resource interface | Dynamic resource loading |
| @Value with ResourceLoaderAware | Simplifies resource injection using @Value annotation | Spring DI advantage |
| ResourceUtils | Provides simple utility methods to access resources as File or URL | When direct file access is required |
| Configuration via Properties | Dynamic file path configuration using properties file | Configuration-driven access |
Conclusion
Reading files from the resources folder in a Spring Boot application is flexible and straightforward. By leveraging Spring's built-in features such as ResourceLoader, @Value, and ResourceUtils, you can efficiently manage application resources. Understanding these approaches will help you choose the right method based on your specific requirements.
Related reading
- Read file from resources folder in Spring Boot
- Read resource text file to String in Java
- Read URL to String in few lines of Java code
- Reading a List from properties file and load with Spring annotation @Value
- Reading a plain text file in Java
- Reading a resource file from within jar
- Read/Write String from/to a File in Android
- Read/write to Windows registry using Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.