Read a File from Resources in Spring Boot

Learn to read a file from the ‘/resources’ folder in a Spring boot application using ClassPathResource, ResourceLoader interfaces and @Value annotation.

1. Spring Resource Interface

Spring’s Resource interface is used for abstracting access to low-level resources. Some of the most important methods from the Resource interface are:

public interface InputStreamSource {
    InputStream getInputStream() throws IOException;

public interface Resource extends InputStreamSource {

    boolean exists();
    boolean isOpen();
    URL getURL() throws IOException;
    File getFile() throws IOException;
    Resource createRelative(String relativePath) throws IOException;
    String getFilename();
    String getDescription();

There are a number of Resource implementations inbuilt in Spring such as ClassPathResource, UrlResource, FileSystemResource, ServletContextResource and InputStreamResource etc.

2. Using ClassPathResource

The ClassPathResource represents a resource obtained from the classpath using the class loader reference. It supports resolution as if the class path resource resides in the filesystem but not for resources in a JAR. It recognizes the special prefix 'classpath:'on the string path.

Resource resource = new ClassPathResource("classpath:data.txt");
File file = resource.getFile();
String content = new String(Files.readAllBytes(file.toPath()));

To read a file from inside a jar or war file (that has not been extracted by the application server), please use resource.getInputStream().

Resource resource = new ClassPathResource("classpath:data.txt");
InputStream inputStream = resource.getInputStream();

try {
    byte[] bdata = FileCopyUtils.copyToByteArray(inputStream);
    String content = new String(bdata, StandardCharsets.UTF_8);;
} catch (IOException e) {
    LOGGER.error("IOException", e);

3. Using @Value

We can also directly inject a classpath resource into a Spring bean using the @Value annotation. Note that it eagerly loads the file.

Resource resource;

//Inside some method
File file = resource.getFile();

4. Using ResourceLoader

Instead of using ClassPathResource, we can also use ResourceLoader for loading resources. File paths can be fully qualified URLs, e.g. "file:C:/test.dat" or "classpath:test.dat". It also supports relative file paths, e.g. "WEB-INF/test.dat".

ResourceLoader resourceLoader;

Resource resource = resourceLoader.getResource("classpath:data.txt");
File file = resource.getFile());

It is meant to be implemented by objects that can return Resource instances. All application contexts implement the ResourceLoader interface, and therefore all application contexts may be used to obtain Resource instances.

private ApplicationContext ctx;

Resource resource = resourceLoader.getResource("classpath:data.txt");
File file = resource.getFile());

Drop me your questions related to the read file from the resources folder in Spring.

Happy Learning !!

Leave a Reply

Most Voted
Newest Oldest
Inline Feedbacks
View all comments

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Our Blogs

REST API Tutorial