Exploring a zip file

This post explains how to explore a zip file. Main Code package ZipDemo; import java.io.IOException; import java.util.Enumeration; import java.util.zip.ZipEntry; import java.util.zip.ZipFile; public class ZipDemo1 { public static void main(String[] args) { try (ZipFile zipFile = new ZipFile(“E:\\Projects\\JavaProjects\\JavaConcepts\\WorkFolder\\Folder1.zip”)){ Enumeration entries = zipFile.entries(); for (Enumeration e = entries; e.hasMoreElements();) { ZipEntry entry = e.nextElement(); System.out.println(entry.getName()); } }…… Continue reading Exploring a zip file

Programmatically generating schema for java classes

This post will explain how to generate schema files for java classes programmatically. The schema is generated by an instance of JAXBContext. In addition to JAXBContext we also need SchemaOutputResolver for this purpose. The SchemaOutputResolver class is an abstract class so we need to create a new class extending SchemaOutputResolver as shown below. package JAXB;…… Continue reading Programmatically generating schema for java classes

Reading json data in streaming way

This post explains how to read json data as a stream. The previous post explains the same using JsonReader. The difference is that to use JsonReader we need to build the entire object (which represents json data) in memory. Whereas with JsonParser we dont need to build the entire object. The below code explains how…… Continue reading Reading json data in streaming way

Loading multiple instances by list of entity ids

From hibernate 5.1 we can load multiple instances of same class type in one shot by using the list of ids. The below code gives an example of this import java.util.List; import org.hibernate.MultiIdentifierLoadAccess; import org.hibernate.Session; import org.hibernate.SessionFactory; import model.Series; public class HibernateDemo { public static void main(String[] args) { SessionFactory sessionFactory = HibernateUtil.createSessionFactory(); Session session…… Continue reading Loading multiple instances by list of entity ids

Writing JSON Data in a streaming way

This post explains how to write json data to a file using JsonGenerator. The previous post explains the same using JsonWriter. The difference is that to use JsonWriter we need to build the entire object (which represents json data) in memory before writing to a file. Whereas with JsonGenerator we dont need to build the…… Continue reading Writing JSON Data in a streaming way

Updating a particular element in JAXB generated xml

Sometimes xml documents are very big and our requirement is to update a particular section of the document. JAXB binder comes to our help. Binder object maintains the mapping between the java object and xml infoset like DOM. Any modification to java object can be synchronized to xml held by binder object and vice versa.…… Continue reading Updating a particular element in JAXB generated xml

Marshalling and Unmarshalling java class

1) First we need to create jaxb context as shown below. A instance of this class is always required, as it is through this class we get marshaller and unmarshaller objects JAXBContext jaxbContext = JAXBContext.newInstance(Country.class); An instance of JAXBContext is thread safe but it is heavy weight object. So it is recommended to reuse the…… Continue reading Marshalling and Unmarshalling java class

How to use SequenceInputStream

SequenceInputStream reads data from multiple inputstreams in an order. It starts with first inputstream and once it is done reading the first inputstream, it starts reading data from the next available inputstream. Two constructors are available for creating an instance of this stream. First one being SequenceInputStream(InputStream s1, InputStream s2) The above constructors limits us…… Continue reading How to use SequenceInputStream

Updating an existing json data

This post explain how we can update an existing json data stored in a file. For example purpose lets create a json file named jsondata1.txt with the following data jsondata1.txt { “firstName”:”John”, ”lastName”:”McClane”, ”age”:”28″, ”address”:{ “street”:”street1″, ”city”:”city1″, ”state”:”state1″, ”country”:”country1″, ”postalCode”:”12345″ }, “Phones”:[{“Mobile”:”111-111-1111″},{“Home”:”222-222-2222″}] } The code is as shown below package objectmodel; import java.io.FileReader; import java.io.FileWriter;…… Continue reading Updating an existing json data

LineNumberReader class setLineNumber method

As mentioned in Java API, LineNumberReader class keeps track of line number and return line number in addition to the data read. In this post I will mainly cover setLineNumber method in the class. The api document doesn’t clearly mention its purpose. The setLineNumber can be used for two purposes 1) Reset the line number…… Continue reading LineNumberReader class setLineNumber method