This discussion is archived
5 Replies Latest reply: Nov 17, 2010 12:53 PM by 802316 RSS

Size problems writing big POJOs in an ObjectOutputStream

741717 Newbie
Currently Being Moderated
Hello Everyone

I'm having a problem writing an obtect to HDD. This object is simple, but during it's use it can grow (has Vectors inside).
When I try to store it, the maximum size of the file it's generated is exactly 16KB.

Here is the source:
    public static void saveData() {
        String dataDir = System.getProperty("user.dir") + File.separatorChar + "config\\";
        try {
            FileOutputStream fos = new FileOutputStream(datosDir + "data.dat");
            ObjectOutputStream oos = new ObjectOutputStream(fos);

            oos.writeObject(formulario);
            oos.flush();
            oos.close();

        } catch (IOException e) {
            System.out.println("Error storing data at " + dataDir + "data.dat");
            System.out.println(e.getMessage());
        }
    }
Could it be possible to store all the class using a bucle or whatever?
Should I use a different approach to save and recover it?

Thanks for your help and suggestions
  • 1. Re: Size problems writing big POJOs in an ObjectOutputStream
    791266 Explorer
    Currently Being Moderated
    Why are you using serialization? What is it that you want to save, and why?
  • 2. Re: Size problems writing big POJOs in an ObjectOutputStream
    741717 Newbie
    Currently Being Moderated
    Hello Kaj

    Thanks for replying

    I use serialization just because it's needed to save this object to the hard drive. The fact is that I'm using a single big file to save all the data of a "form" that gets filled using the app. When the app gets closed, this big object is saved to the HDD so it can be restored the next time the app gets opened, so the user will be able to resume his work as he left it.
  • 3. Re: Size problems writing big POJOs in an ObjectOutputStream
    sabre150 Expert
    Currently Being Moderated
    You really should post your actual code and not some cut down version - there are at least two typos in the code you have published.

    I have just corrected your code and added some data to write.
     public static void saveData()
        {
    
            ArrayList<Integer> formulario = new ArrayList<Integer>();
            for (int i = 0; i < 1000000; i++)
                formulario.add(i);
    
            String dataDir = System.getProperty("user.home") + File.separatorChar + "config" + File.separatorChar;
            try
            {
                File dataFile = new File(dataDir + "data.dat");
                dataFile.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(dataFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
    
                oos.writeObject(formulario);
                oos.flush();
                oos.close();
    
            } catch (IOException e)
            {
                System.out.println("Error storing data at " + dataDir + "data.dat");
                System.out.println(e.getMessage());
            }
        }
    This produces a file of about 10 MBytes!

    P.S. That exception handling is dreadful.
  • 4. Re: Size problems writing big POJOs in an ObjectOutputStream
    741717 Newbie
    Currently Being Moderated
    Hello Sabre150

    Thanks for your reply

    Following your example I tried something that, following my logic, shouldn't work... but it worked!
    /*
     * To change this template, choose Tools | Templates
     * and open the template in the editor.
     */
    
    package javaapplication2;
    
    import java.io.File;
    import java.io.FileOutputStream;
    import java.io.IOException;
    import java.io.ObjectOutputStream;
    import java.io.Serializable;
    import java.util.ArrayList;
    
    /**
     *
     * @author INF026
     */
    public class Main {
    
        /**
         * @param args the command line arguments
         */
        public static void main(String[] args) {
    
            Formulario formulario = new Formulario(1000000);
    
            String dataDir = System.getProperty("user.home") + File.separatorChar + "config" + File.separatorChar;
            try
            {
                File dataFile = new File(dataDir + "data.dat");
                dataFile.getParentFile().mkdirs();
                FileOutputStream fos = new FileOutputStream(dataFile);
                ObjectOutputStream oos = new ObjectOutputStream(fos);
    
                oos.writeObject(formulario);
                oos.flush();
                oos.close();
    
            } catch (IOException e)
            {
                System.out.println("Error storing data at " + dataDir + "data.dat");
                System.out.println(e.getMessage());
            }
        }
    
        private static class Formulario implements Serializable {
            private ArrayList<Integer> formulario = new ArrayList<Integer>();
    
            public Formulario() {
            }
    
            private Formulario(int i) {
            for (int j = 0; j < i; j++)
                formulario.add(i);
            }
    
        }
    }
    A final answer to mi question would be that there it isn't any real problem storing big objects using ObjectOutputStream,
    So, mi error must be another thing. I will review mi code more carefully to find where is the mistake.

    Thank you very much!
  • 5. Re: Size problems writing big POJOs in an ObjectOutputStream
    802316 Pro
    Currently Being Moderated
    ObjectOutputStream is a fairly verbose format which makes it a good candidiate for compression. If your file is more than a few KB, you should get a good compression ration and its vary easy to add it to your code.
    ObjectOutputStream oos = new ObjectOutputStream(new GZIPOutputStream(fos));
    However, even 16 MB of disk space is fairly trivial these days, i.e. << 1 cent of disk space

Legend

  • Correct Answers - 10 points
  • Helpful Answers - 5 points