APerson
APerson

Reputation: 712

Most compressed way to store String Object

I have A String object that I want to save to a file, and I dont care if it is saved as plain text or binary. I have tried saving as plain text and it was about 27 bytes. I then tried ObjectOutputStream and it was 24 bytes. Is there any better way of saving String objects to a file? The string is 189:25:600:-324324214& and intend to have thousands of them. Thats why I want it compressed, and of course each String will be a bit different

Upvotes: 1

Views: 513

Answers (2)

Sage
Sage

Reputation: 15418

Followings are some option to use for compressing:

  1. GZIPOutputStream ("deflate in gzip wrapper")
  2. DeflaterOutputStream ("plain deflate", recommend over gzip or zip "wrappers") standard
  3. LZMA Java implementations
  4. jZlib
  5. LZO-Java
  6. Lz4-java and LZ4-HC (Lz4 fast with reasonable compression ratio, LZ4-HC is for high compression)
  7. XZ (incorporates the LZMA2 compression algorithm)
  8. snappy-java (used JNI, developed by Google based on ideas from LZ77)

Well i have only used 1,2,4 and 7 until now :). using XZ seems reasonable to me, fast and higher compression ration and very easy to use.

XZOutputStream out = new XZOutputStream(outstream, LZMA2Options);
InputStream in = new XZInputStream(anInputstream);

Upvotes: 2

Ted Hopp
Ted Hopp

Reputation: 234807

Wrap a FileOutputStream inside a DeflatorOutputStream inside an ObjectOutputStream to create the file. (Write the entire array as a single object.) Then wrap a FileInputStream inside an InflatorInputStream inside an ObjectInputStream to read.

Upvotes: 2

Related Questions