Sunday, 11 June 2017

Java Tutorial : Java IO (Java File - How to compress serialized object into a file - Gzip) ~ foundjava


Click here to watch in Youtube : 

Person.java 
import java.io.Serializable;

public class Person implements Serializable
{
    private static final long serialVersionUID = 3069227031912694124L;
    private String name;
    private int age;

    public Person(String name, int age)
    {
        super();
        this.name = name;
        this.age = age;
    }

    public String getName()
    {
        return name;
    }

    public void setName(String name)
    {
        this.name = name;
    }

    public int getAge()
    {
        return age;
    }

    public void setAge(int age)
    {
        this.age = age;
    }

}
GZipSerializeDemo.java 
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectOutputStream;
import java.util.zip.GZIPOutputStream;

/*
 * We can compress the serialized object to
 * reduce the file size.
 */
public class GZipSerializeDemo
{

    public static void main(String[] args) throws IOException
    {
        GZipSerializeDemo gzipSerializeDemo = new GZipSerializeDemo();
        String gzipPath = "D:/work/person.gz";
        gzipSerializeDemo.gzipPersonObject(gzipPath);
    }

    public void gzipPersonObject(String gzipPath) throws IOException
    {

        Person person = new Person("Peter", 45);
        /*
         * If the Streams are within the
         * "try-With-Resources" block, then it will be
         * closed automatically.
         */
        try (FileOutputStream fos = new FileOutputStream(gzipPath);
                GZIPOutputStream gz = new GZIPOutputStream(fos);
                ObjectOutputStream oos = new ObjectOutputStream(gz);)

        {

            oos.writeObject(person);
            System.out.println("Person object is serialized and compressed.");

        }

    }
}
Output
Person object is serialized and compressed.

GZipDeserializeDemo.java
import java.io.FileInputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.zip.GZIPInputStream;

/*
 * How to decompress serialized object from a Gzip file.
 */
public class GZipDeserializeDemo
{

    public static void main(String[] args) throws IOException,
            ClassNotFoundException
    {
        GZipDeserializeDemo gzipDeserializeDemo = new GZipDeserializeDemo();
        String gzipPath = "D:/work/person.gz";
        Person person = gzipDeserializeDemo.deserializePersonObject(gzipPath);
        System.out.println("Name : " + person.getName());
        System.out.println("Age : " + person.getAge());
    }

    public Person deserializePersonObject(String gzipPath)
            throws ClassNotFoundException, IOException
    {
        /*
         * If the Streams are within the
         * "try-With-Resources" block, then it will be
         * closed automatically.
         */
        try (FileInputStream fin = new FileInputStream(gzipPath);
                GZIPInputStream gis = new GZIPInputStream(fin);
                ObjectInputStream ois = new ObjectInputStream(gis);)
        {

            Person person = (Person) ois.readObject();

            return person;

        }

    }
}
Output
Name : Peter
Age : 45

Click the below link to download the code:

CLICK HERE

No comments:

Post a Comment