Solved

Most compact way to store numbers in a file?

Posted on 2010-08-13
12
359 Views
Last Modified: 2012-06-21
Hi,

I want to create a file which uses as little space as possible. The contents of the file will be numbers, ranging from 0 to 1,000,000,000,000, and then ascii alphas.

The format of the file will look something like this:

   abc:0,800,543,32|
   def:24,32,900,50,8192,234|
   
and so on (there will be no line breaks, it will be one long stream). Would representing the numbers as a single char per digit be more efficient than representing the entire number as an int? I mean storing the number "50" requires two bytes, one for each digit, as characters. If I wrote it as an int, it will require 32 bytes, right?

Thanks

0
Comment
Question by:DJ_AM_Juicebox
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
  • 2
  • +4
12 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 33432880
Try using DataOutputStream
0
 

Author Comment

by:DJ_AM_Juicebox
ID: 33432912
Yeah I've used DataOutputStream but am not certain on this point:

     void writeByte(int v)
     Writes out a byte to the underlying output stream as a 1-byte value.

I don't understand the explanation - certainly an int can't be written as a single byte, won't it require 32 bytes (platform dependent). Or does it do what I'm proposing, writes each character as an ascii character, so one byte per character used?

Thanks
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 33432983
This is what javadocs tells us:

Writes to the output stream the eight low- order bits of the argument v.
The 24 high-order bits of v are ignored. (This means that writeByte does
exactly the same thing as write for an integer argument.) The byte
written by this method may be read by the readByte method of interface
DataInput, which will then return a byte equal to (byte)v.
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 

Author Comment

by:DJ_AM_Juicebox
ID: 33433036
Oh this is what I don't get though - if the high-order 24 bits are ignored - how does it capture the value of the number correctly? If my int is equal to Integer.MAX_VALUE, isn't it using all the bits?

Thanks
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 33433127
What is means is - it stores the byte part (first 8-bits) and ignores
rest of them. So, you should not use this method to store integers, you
should use writeInt instead
0
 
LVL 92

Expert Comment

by:objects
ID: 33433959
you can't use writeByte(), you need to use writeInt() to write ints
writing as a string will only be efficient for small values
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33434112
>> If I wrote it as an int, it will require 32 bytes, right?

You mean 32 bits, but actually that range needs long. You can always apply compression to the result
0
 
LVL 5

Expert Comment

by:avya2k
ID: 33435572
I think It would store 32 bits per ASCII char so whatever is the data in your string, you would get bytes size equals to number of chars in string.
If you are concerned about only the size of file you can use zip utilities from JAVA to compress data.
If you are concerned about FAST Read Write, instead of using IO Streams directly,  try using Serialization and create an serialized object to get the data for you if possible
OR use BYTE Streams instead of char streams like INPUTStreams and OUTPUTStreams as those are faster than Char streams
0
 
LVL 7

Expert Comment

by:HalfAsleep
ID: 33438265
In Java, an int is always 32 bits, which is 4 bytes.

A short is always 16 bits, which is 2 bytes.

So as you can see, it is always more efficient to write the int itself instead of the ascii string representation, unless you can guarantee that most of your numbers will be smaller than 9999 for int and 99 for short.

I would therefore make the file binary, as in write the int/short directly to file, instead of making the file ascii.
0
 
LVL 4

Expert Comment

by:msk_apk
ID: 33443020
ByteArrayOutputStream can be used achived this if you are able to convert your data into bytes. Otherwise DataOutputStream can be used for all objects that implements Serializable interface.
0
 
LVL 86

Accepted Solution

by:
CEHJ earned 500 total points
ID: 33443409
fyi, writing a c 25MB file full of numbers with DataOutputStream, some byte, some long, it's possible to achieve a deflation ratio of 0.84 with gzip compression
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 33561326
:-)
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
Viewers learn about the scanner class in this video and are introduced to receiving user input for their programs. Additionally, objects, conditional statements, and loops are used to help reinforce the concepts. Introduce Scanner class: Importing…

728 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question