views:

37

answers:

2

hi there,

i want to save a lot of binary data files(pdf, images, office docs...) into one single "blob" file. now i'm not sure what is better, saving stuff with java Serializable and save things to disc or to use a database like sqlite to make things happen. or is there a third, maybe better way? my question is, how well do those methods perform in terms of access speed and data-integrety. in this one single blob file there will be one day maybe a hundreds of pdf files in. what way would you prefer?

+1  A: 

Definitely don't use java.io.Serializable. Just write binary data as-is to its target. Serializing would only add unnecessary overhead and would make the saved data unusable for other tools than Java.

I also wouldn't push it all in a single field in a single row. It makes it all tight coupled and storing/retrieving the individual entries may be more expensive. Rather store each in its own row. You can if necessary link/reference the one and other by another column with a foreign key.

Now the Java code, the JDBC API offers PreparedStatement#setBinaryStream() to save binary data in flavor of an InputStream into the database. There's also the setBytes() method for the case you've it in a (memory hogging) byte[]. Then, to retrieve it, you can just use either ResultSet#getBinaryStream() or getBytes().

You can on the other hand also just store those files in the local disk file system the usual Java IO way using FileOutputStream and read them using FileInputStream. You can if necessary store file paths/names in the database. This decouples the binary data from the database which makes it less portable, but better reuseable for other purposes.

See also

BalusC
+2  A: 

Assuming you want to create one big file (and not some kind of database entry), you might want to use the java.util.zip package to create a ZIP file of those PDFs, preferably uncompressed (because it's faster and PDFs usually don't compress well).

By doing that, you can easily use third-party-tools to extract those PDFs from your "blob file", i.e. zip archive.

ammoQ