Thursday, 24 November 2016

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


Click here to watch in Youtube : 
https://www.youtube.com/watch?v=dKN02FjwfvE&list=UUhwKlOVR041tngjerWxVccw

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:
https://sites.google.com/site/ramj2eev1/home/javabasics/JavaIODemo_Gzip_Serialize_App.zip?attredirects=0&d=1

Github Link:
https://github.com/ramram43210/Java/tree/master/BasicJava/JavaIODemo_Gzip_Serialize_App

Bitbucket Link:
https://bitbucket.org/ramram43210/java/src/97750dcb5a5d126b3956b792a4fc850ca4dd67ca/BasicJava/JavaIODemo_Gzip_Serialize_App/?at=master

See also:
  • All JavaEE Viedos Playlist
  • All JavaEE Viedos
  • All JAVA EE Links
  • Servlets Tutorial
  • All Design Patterns Links
  • JDBC Tutorial
  • Java Collection Framework Tutorial
  • JAVA Tutorial
  • Kids Tutorial
  • No comments:

    Post a Comment

    Google+ Followers

    Google+ Badge