In this post under Apache Commons IO, I will show with example how to read the contents of file present in your projects classpath. “IOUtils” class provides a static method named “resourceToString”. This method reads the contents of the file and return a String object. This method takes 3 arguments1) the file name2) the ClassLoader…… Continue reading Reading classpath file contents
Month: April 2025
Reading classpath file contents as bytes
In this post under Apache Commons IO, I will show with example how to read the contents of file present in your projects classpath as bytes. “IOUtils” class provides a static method named “resourceToByteArray”. This method reads the contents of the file as bytes. This method takes two arguments1) the file name2) the ClassLoader instance…… Continue reading Reading classpath file contents as bytes
Reading file contents using Iterator
In this post under Apache Commons IO, I will show with example how to read file contents using Iterator. “IOUtils” provides a static method named “lineIterator” which reads the file and returns an “Iterator” object. Each item returned by this iterator represent a line in the file. Using the “Iterator” object we can iterate the…… Continue reading Reading file contents using Iterator
MappingConstants.ANY_REMAINING
In this post under MapStruct, I will explain with example the purpose of “MappingConstants.ANY_REMAINING” enum constant. This enum constant is basically used when mapping source enum constant to destination enum constant. This enum constant is used in mapping interface to represent enum constants which don’t have explicit “@ValueMapping” annotation. Let me explain with an example…… Continue reading MappingConstants.ANY_REMAINING
List Fill demo
In this post under Java Collections, I will explain with example the purpose of “fill” static method in “Collections” utility class. This method takes any list as input and replace all the elements of the list with user given object. So if we have a list named “entries” containing below elements [1, 2, 3, 4,…… Continue reading List Fill demo
List nCopies demo
In this post under Java Collections, I will explain with example the purpose of “nCopies” static method in “Collections” utility class This method creates a list containing n copies of input object. Where n and input object are user specified. So basically this method takes two arguments.1) The object to be replicated in the list2)…… Continue reading List nCopies demo
MappingConstants.NULL example
In this post under MapStruct, I will explain with example the purpose of MappingConstants.NULL enum constant. In all my previous posts, I explained how to map a source enum constant to destination enum constant. What if the client calling the code (that will map passed source enum constant to destination enum constant) passes a null…… Continue reading MappingConstants.NULL example
Elvis operator
In this post under Spring SpEl, I will explain with example the purpose of Elvis operator. Lets say you have below statement written using ternary operator String result = (data != null) ? data : null; In the above statement “data” variable is of type String. So we are checking if “data” is not null,…… Continue reading Elvis operator
List rotation demo
In this post under Java Collections, I will explain with example how to rotate elements in a list. Java “Collections” utility class has static method named “rotate” which will rotate elements in a list. This method takes two arguments.1) the list2) integer representing the distance Below is the complete main class for your reference Main…… Continue reading List rotation demo
MappingConstants.THROW_EXCEPTION example
In this post under MapStruct, I will explain with example the purpose of enum “MappingConstants.THROW_EXCEPTION”. This enum is mainly used when mapping source enum constant to destination enum constant. If you want to throw an exception for some source enum constant. We can use this enum constant. Below is the example showing the usage. Lets…… Continue reading MappingConstants.THROW_EXCEPTION example