A Convenient Way to Read Resources in Java

A Convenient Way to Read Resources in Java

Resources lay on the classpath of Java application, not on the filesystem. You almost never will have IOException, unless you made a mistake in your code. Loading of resources is closer to class import than to reading of file from the filesystem. You don’t surround your imports with try-catch, do you ?

I am working at Elasticsearch expert company and in my daily life, I see a lot of JSON: requests, responses, mappings, settings, etc. In 90% of cases, the functionality of software we make involves the manipulation of JSON. And it’s pretty common for us to have JSON files inside resources of Java code.

One day, after writing code to load and parse JSON for 100500th time I understood one thing: reading content of resources in Java is harder than it should be!

Just take a look. Let’s assume we have a resource com/adelean/junit/jupiter/resource.txt containing text:

“The quick brown fox jumps over the lazy dog.”

This is one of existing ways to load its content:

Looks scary, doesn’t it? Need to open and close multiple streams/readers, handle IOException, all just to get text content.

This Stackoverflow topic (https://stackoverflow.com/questions/15749192/how-do-i-load-a-file-from-resource-folder), and many others, explain how to load resources. You can give it a look, or better, to skip it and continue to read this article.

Luckily for us, there are great libraries like google Guava, that significantly simplify the task. Here is the same operation made with Guava:

Even if it’s simpler, it is still too complicated for a task that probably must be done with one line. And we still have to handle exceptions.

I always found it surprising why we must handle exceptions while loading java resources. While exception handling is justified for the files, because file can be absent, or doesn’t have right permissions, the situation is different for resources.

*Resources lay on the classpath of Java application, not on the filesystem. *You almost never will have IOException, unless you made a mistake in your code. Loading of resources is closer to class import than to reading of file from the filesystem. You don’t surround your imports with try-catch, do you ?

load parse java spring resources

Bootstrap 5 Complete Course with Examples

Bootstrap 5 Tutorial - Bootstrap 5 Crash Course for Beginners

Nest.JS Tutorial for Beginners

Hello Vue 3: A First Look at Vue 3 and the Composition API

Building a simple Applications with Vue 3

Deno Crash Course: Explore Deno and Create a full REST API with Deno

How to Build a Real-time Chat App with Deno and WebSockets

Convert HTML to Markdown Online

HTML entity encoder decoder Online

How to Install OpenJDK 11 on CentOS 8

What is OpenJDK? OpenJDk or Open Java Development Kit is a free, open-source framework of the Java Platform, Standard Edition (or Java SE).

Spring Beans in Java

Spring IoC container is the core of the Spring Framework.In spring-based applications, objects live inside a spring containerThe container instan.

Spring AOP Tutorial | Spring AOP Example In Java | Java Spring Tutorial For Beginners

This video on "Java Spring AOP Tutorial For Beginners" will help the learners with a detailed briefing of the Spring AOP concepts. This Spring AOP Tutorial will include a practical session for a better learning experience.

Java Spring Boot First App

Step by Step to your First Spring App

JdbcTemplate Querying Spring — Java

A bunch of examples of using jdbcTemplate in Spring framework (Java)