Spring Boot Read File from resources using ResourceUtils

Introduction

In this Java Spring Boot tutorial, we learn how to use ResourceUtils class to read resource files located in the resources folder of a Spring Boot application.

Create New Spring Boot Project

Open Spring Tool Suite IDE, select menu File > New > Spring Starter Project.

On the New Spring Starter Project popup input new project information as below and click Next.

  • Name: spring-boot-resourceutils
  • Group: dev.simplesolution
  • Artifact: spring-boot-resourceutils
  • Version: 1.0.0
  • Description: Spring Boot Read File from resources using ResourceUtils
  • Package: dev.simplesolution

Spring Boot Read File from resources using ResourceUtils

On the New Spring Starter Project Dependencies popup click Finish.

Spring Boot Read File from resources using ResourceUtils

You can also creating new Spring Boot project using Spring initializr online tool at start.spring.io

Create Sample File in resources folder for Testing

Add a new text file to the resources folder named data.txt with content as below for testing purposes.

/src/main/resources/data.txt

Simple
Solution
https://simplesolution.dev/

How to use ResourceUtils class to read file from classpath

Create a new class named TestReadFile and use ResourceUtils class to read data.txt file then log the file content as the following code.

TestReadFile.java

package dev.simplesolution;

import java.io.File;
import java.nio.file.Files;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Component;
import org.springframework.util.ResourceUtils;

@Component
public class TestReadFile implements CommandLineRunner {
	
	private Logger logger = LoggerFactory.getLogger(TestReadFile.class);

	@Override
	public void run(String... args) throws Exception {
		File file = ResourceUtils.getFile("classpath:data.txt");
		if(file.exists()) {
			byte[] fileData = Files.readAllBytes(file.toPath());
			String fileContent = new String(fileData);
			
			logger.info("data.txt file content:");
			logger.info(fileContent);
		}
	}
}
Run the application, we can see the log messages showing the file content.
  .   ____          _            __ _ _
 /\\ / ___'_ __ _ _(_)_ __  __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
 \\/  ___)| |_)| | | | | || (_| |  ) ) ) )
  '  |____| .__|_| |_|_| |_\__, | / / / /
 =========|_|==============|___/=/_/_/_/
 :: Spring Boot ::                (v2.4.3)

2021-03-03 21:18:24.298  INFO 11568 --- [           main] d.s.SpringBootResourceutilsApplication   : Starting SpringBootResourceutilsApplication using Java 1.8.0_231 on SS with PID 11568 (D:\SimpleSolution\spring-boot-resourceutils\bin\main started by SS in D:\SimpleSolution\spring-boot-resourceutils)
2021-03-03 21:18:24.300  INFO 11568 --- [           main] d.s.SpringBootResourceutilsApplication   : No active profile set, falling back to default profiles: default
2021-03-03 21:18:24.578  INFO 11568 --- [           main] d.s.SpringBootResourceutilsApplication   : Started SpringBootResourceutilsApplication in 0.519 seconds (JVM running for 1.413)
2021-03-03 21:18:24.580  INFO 11568 --- [           main] dev.simplesolution.TestReadFile          : data.txt file content:
2021-03-03 21:18:24.580  INFO 11568 --- [           main] dev.simplesolution.TestReadFile          : Simple
Solution
https://simplesolution.dev/

Final Application Source Code

Finally we have the complete Spring Boot project with code structure as below.

Spring Boot Read File from resources using ResourceUtils

Download Source Code

The source code in this article can be found at: github.com/simplesolutiondev/spring-boot-resourceutils

or clone at:

git clone https://github.com/simplesolutiondev/spring-boot-resourceutils.git

or download at:

Download Source Code

Happy Coding 😊