In this post under java, I will show how to browse a jar file programmatically. For the example I will browse the mockito jar file. So I will add the “mockito-core-2.11.0.jar” in the classpath of the java file. Below is the complete code for browsing the jar file programmatically Main Code 1 package jar; 2…… Continue reading Exploring jar files programmatically
Category: Java
Sorting a section of an array
In this post I will explain how to sort a section of an array. The java Arrays class provides a static method named “sort” which takes the below three arguments 1) the array 2) fromIndex 3) toIndex This method sorts a section of the array. The section to be sorted is indicated by fromIndex (position…… Continue reading Sorting a section of an array
Combining two or more Consumer Functional Interface
In this post I will explain how to combine two or more Consumer Functional interface implementations. Consumer Functional interface has a default method “andThen”, which combines two Consumer Functional interface implementations. The method signature is as shown below default Consumer andThen(Consumer after) The way two Consumer implementations are combined using “andThen” method is as shown…… Continue reading Combining two or more Consumer Functional Interface
Sorting arrays using Comparator
This post explains how to sort arrays using comparator with an example. The java jdk provides Arrays class, which has a static method named “sort”. This method takes two arguments 1) the array to be sorted 2) Comparator which contains the logic to sort the array Below is the example which sort cards based on…… Continue reading Sorting arrays using Comparator
Creating array dynamically
In this post I will explain how to create array dynamically with an example. This is useful when we don’t know the type of array until runtime. Main code 1 package reflection; 2 3 import java.lang.reflect.Array; 4 5 public class ArraysDemo { 6 public static void main(String[] args) { 7 //Creating single dimension array 8…… Continue reading Creating array dynamically
Creating a temporary file and directory
The post shows you how to create a temporary file and directory in java with an example. Main Code 1 package nio; 2 3 import java.io.IOException; 4 import java.nio.file.Files; 5 import java.nio.file.Path; 6 import java.nio.file.Paths; 7 8 public class Example1 { 9 public static void main(String[] args) throws IOException { 10 Path path = Files.createTempFile(“prefix”,…… Continue reading Creating a temporary file and directory
Logging to database using java logging framework
This post explains how to log messages to the database. Create a table with below schema CREATE TABLE logging ( loggername VARCHAR(50) NULL DEFAULT NULL, threadId INT(11) NULL DEFAULT NULL, loglevel VARCHAR(50) NULL DEFAULT NULL, logmessage VARCHAR(50) NULL DEFAULT NULL ) Create a custom handler which will log the message to the database. In our…… Continue reading Logging to database using java logging framework
Setting Filter at Logger level
In the post “Setting Filter at Handler level”, I showed how to set filter at handler level. In this post I will explain how to set filter at logger level. An use case of setting filter at logger level, is restricting logging of messages based on the user who logged it. Custom Filter 1 package…… Continue reading Setting Filter at Logger level
Setting Filter at Handler level
Java logging provides log levels at logger and handler level, to filter the messages. To make sure only required messages are logged. We can extend this functionality and provide our own filter, which will filter messages based on message’s metadata in addition to filtering by log levels. We can add this filter at logger level…… Continue reading Setting Filter at Handler level
Supplier Functional Interface Example
Java 8 added a new functional interface named “Supplier”. The purpose of this interface is to supply instances. I will be explaining using the below example. In the example I have want of list of employees. I will separate the logic behind employee instance creation and adding to list. The employee instance will be created…… Continue reading Supplier Functional Interface Example