Read a File From the Resources Folder in Spring Boot

When working with Spring Boot applications, we often need to place some data into a file and later read from it. We usually put these files into the resources folder.

In this lesson, you will learn to read a file from the resources folder in Spring Boot and we will cover the following ways:

  • Using the ClassPathResource class
  • Using the ResourceLoader interface

Let’s put the file data.txt into the src/main/resources folder:

some data in a file

Read a File from the Resource Folder using ClassPathResource

The ClassPathResource class contains useful methods for reading the resources in a Spring application.

Example

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.util.FileCopyUtils;

import java.io.*;
import java.nio.charset.StandardCharsets;

@SpringBootApplication
public class DemoApplication {

  public static void main(String[] args) throws IOException {
    SpringApplication.run(DemoApplication.class, args);
    readFile();
  }

  public static void readFile() throws IOException {

    // read a file
    Resource resource = new ClassPathResource("classpath:data.txt");

    // get inputStream object
    InputStream inputStream = resource.getInputStream();

    // convert inputStream into a byte array
    byte[] dataAsBytes = FileCopyUtils.copyToByteArray(inputStream);

    // convert the byte array into a String
    String data = new String(dataAsBytes, StandardCharsets.UTF_8);

    // print the content
    System.out.println(data);

  }
}
Output: some data in a file

Read a File Using the ResourceLoader Interface

The ResourceLoader interface is used for loading resources (e.g., class path or file system resources). An ApplicationContext is required to provide this functionality plus extended ResourcePatternResolver support.

For this example, we will create a RestController class and autowire the ResourceLoader:

Example

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.core.io.Resource;
import org.springframework.core.io.ResourceLoader;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;

@RestController
public class HomeController {

  @Autowired
  ResourceLoader resourceLoader;

  @GetMapping("/")
  public String getContentFromFile() throws IOException {

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

    InputStream inputStream = resource.getInputStream();

    byte[] dataAsBytes = FileCopyUtils.copyToByteArray(inputStream);

    String data = new String(dataAsBytes, StandardCharsets.UTF_8);

    return "data";

  }
}
Output: some data in a file
 
That was all about how to read a file from the resources folder in Spring Boot. Proceed to the next lesson.
 
Happy coding!

Leave a Reply

Your email address will not be published. Required fields are marked *