3
\$\begingroup\$

I have my own type of object, which implements Serializable. Here I have one called "extendedFormat" (not a terribly helpful name, yes, I know). I'm writing it out to a file like so:

try {
    // is this the best way to do this????????
    // this seems like too many streams perhaps.
    // like maybe this is now a river
    File file = new File("C:/temp/tempExport.bef");
    file.createNewFile();
    FileOutputStream fileStream = new FileOutputStream(file);
    BufferedOutputStream bufferStream = new BufferedOutputStream(fileStream);
    ZipOutputStream zipStream = new ZipOutputStream(bufferStream);
    zipStream.putNextEntry(new ZipEntry("bef"));
    ObjectOutputStream objectStream = new ObjectOutputStream(zipStream);
    objectStream.writeObject(extendedFormat);
    zipStream.closeEntry();
    objectStream.close();
    zipStream.close();
    bufferStream.close();
    fileStream.close();
} catch (FileNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

Is this the best way to do this? It seems a little over complicated but I want to save as much time writing it out and also space in terms of how big the file is that I write out.

I know this may seem like a lot for one object, but it can hold an entire table with possibly hundreds of columns and thousands of rows, so efficiency is important.

\$\endgroup\$
  • 1
    \$\begingroup\$ Do you have any hard requirements to use Java < 8, or would you consider using a library for this? \$\endgroup\$ – Panayiotis Poularakis Jul 31 '17 at 18:55
  • \$\begingroup\$ @PanayiotisPoularakis no, a library would be fine as long as it is one I can use in a commercial application. \$\endgroup\$ – MMAdams Jul 31 '17 at 18:57
2
\$\begingroup\$

You could use try-with-resources to efficiently close the streams, this also reduces line count and readability.

IOUtils provides a tested utility to copy between input and output streams, add commons-compress to you classpath, for example using gradle

 compile group: 'org.apache.commons', name: 'commons-compress',version: '1.14'

I simply extended the code you provided. I believe you will get the idea for reading back the zip file.

  try {

            File file = new File("C:/temp/tempExport.bef");
            file.createNewFile();

//Use try with resources to handle closing streams

            try (ZipOutputStream zipStream = new ZipOutputStream(new BufferedOutputStream(new FileOutputStream(file)))) {
                zipStream.putNextEntry(new ZipEntry("bef"));
                ObjectOutputStream objectStream = new ObjectOutputStream(zipStream);
                objectStream.writeObject(extendedFormat);
            }

         //to read the file you need a temp buffer or file to decompress the archive
            File temp = Files.createTempFile("", "").toFile();

            try (ZipInputStream zis = new ZipInputStream(new BufferedInputStream(new FileInputStream(file)));
                 BufferedOutputStream bos = new BufferedOutputStream(new FileOutputStream(temp));
                 BufferedInputStream bis = new BufferedInputStream(new FileInputStream(temp))) {
                zis.getNextEntry();
                IOUtils.copy(zis, bos);
                bos.close();
                ObjectInputStream ois = new ObjectInputStream(bis);
                ExtendedFormat extendedFormat= (ExtendedFormat) ois.readObject();
            }

            temp.delete();

        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.