Creating a file archiver in zip format

This post explains how to create file archiver in WinZip format. WinZip is a file archiver and compression tool. Whenever a zip file is created by default the file is compressed and archived together in a single file. We can change the default behaviour (i.e., compression) by setting the level property in ZipOutputStream. The below…… Continue reading Creating a file archiver in zip format

Creating a zip file of a folder with CRC32 checksum

This post explains how to create a zip file of a folder (also containing sub folders) with CRC32 checksum. The below code is similar to the previous post “Creating zip file of a folder” with minor change in main method. The main method code snippet from the previous post as shown below File folder =…… Continue reading Creating a zip file of a folder with CRC32 checksum

Printing json data in a formatted way

This post explains how to print json data in a properly formatted way, whether the destination is console, file etc. For example when the below code prints the json data to a file, the output will be as shown below Initial Code package package11; import java.io.File; import java.io.FileWriter; import java.io.IOException; import com.fasterxml.jackson.core.JsonFactory; import com.fasterxml.jackson.core.JsonGenerator; public…… Continue reading Printing json data in a formatted way

Handling json deserialization error

This post explains how to handle errors generated while deserializing the json file. This can be achieved by implementing an interface “DeserializationProblemHandler” provided by jackson framework as shown below UnMarshallingErrorHandler package package1; import java.io.IOException; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; import com.fasterxml.jackson.databind.deser.DeserializationProblemHandler; public class UnMarshallingErrorHandler extends DeserializationProblemHandler { @Override public boolean handleUnknownProperty(DeserializationContext ctxt,…… Continue reading Handling json deserialization error

Executing compiled script

In the previous posts under “Java Scripting”, we used ScriptEngine eval method to execute scripts stored in a string variable or file. The ScriptEngine’s eval methods takes the script compiles to intermediate code and execute it immediately. If we have a script which has to be executed repeatedly, then instead of compiling and executing it…… Continue reading Executing compiled script

Combining Predicates

This post explains how we can combine two or more predicates using logical operations as mentioned below 1) and 2) or 3) negate Below is an example Person package Function; public class Person { private String ssn; private String fname; private String lname; private String description; private int age; public String getSsn() { return ssn;…… Continue reading Combining Predicates

Getting JsonGenerator settings

This post explains how to get JsonGenerator settings. JsonGenerator class obtained from JsonFactory is responsible for writing json data as a stream instead of constructing an entire object model in memory and then writing to destination. The list of settings that can be turned on or off are present in an enumeration JsonGenerator.Feature. The below…… Continue reading Getting JsonGenerator settings

Getting JsonFactory settings

This post explains how to get JsonFactory settings. The JsonFactory class is thread safe and responsible for creating instances of writer and reader. Creating an instance of JsonFactory is an light weight operation. The list of settings that can be turned on or off are present in an enumeration JsonFactory.Feature. The below code shows how…… Continue reading Getting JsonFactory settings