In this post, we are going to check How to Load a File From Classpath In Spring Boot. Long back I was working on a very simple project.

We realised Reading a property source in Spring boot is easy and it can get tricky as well .

As Spring works on the concept of fat jar it is  good to know for developer the best ways to load a file from classpath  in spring boot .

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

Learn about creating Microservices using Spring Boot and Spring Cloud

I wanted to read a JSON file from the resources folder.src/main/resources Hence written a code something like this below.

	
@Value("classpath:test.json")
private Resource resource;
File file = resource.getFile();

Now, this code works completely fine once I run it using ,mvn:spring-boot:run but as soon I’m building and packaging the application using maven and running it as simple executable jar I’m getting an exception. Let’s go ahead and find out the solution now.

Must Read

Using InputStream  

We can use InputStream to read a file from classpath in your Spring boot application . Let’s define the name of json file to be read with Resource .
        
        @Value("classpath:test.json")
	Resource resourceFile;
Let’s See How We can read the File , We will now show you actual code to Write and retrieve the content of file from your spring boot based application.
	
    private void testResource(Resource resource) {
		try {
			InputStream resourcee = resource.getInputStream();
			String text = null;
			try (final Reader reader = new InputStreamReader(resourcee)) {
				text = CharStreams.toString(reader);
			}
			System.out.println(text);
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
We have seen How we can Load a File From classpath In Spring Boot If the file is inside a jar.

Reading as a File:-

There is another way to load resources in spring boot. We are now going to check how we can load a file from classpath in spring boot if the file is present in the filesystem not inside a jar.
	
	public void testResourceFile() throws IOException {
		File resource = new ClassPathResource("test.json").getFile();
		String text = new String(Files.readAllBytes(resource.toPath()));
	}

Reading Resource as ResourceLoader:-

ResourceLoader is used to loads resources from class-path as well as file system. It is one of the very commonly used way to read a file from classpath .
@Autowired
ResourceLoader resourceLoader;
Resource resource=resourceLoader.getResource("classpath:test.json");
Now we can use resource.getInputStream() or resource.getFile() as required.

Using ResourceUtils:-

Load a file from classpath in spring boot using ResourceUtils resolves the given resource location to a,java.io.File i.e. to a file in the file system.It doesn’t check if the file exists or not. It is my most preferred way of doing it in any spring boot based application i am working .
ResourceUtils.getFile("classpath:test.json");