18

I'm trying to write data to a file in binary format for compression. The data consists entirely of floating points so I decided to quantize the data to an intergers between 0 and 65535 so the data can be written as two bit unsigned integers and ultimately save space. However, I need to output that quantized data to a file in binary instead of human-readable Ascii.

At the moment this is what I'm doing

@param outputFile the file containing the already quantized data as strings in a .txt file

public void generateBinaryRioFile(String materialLibrary,
        String outputFile, String group, String mtlAux) {

    try {

        // Create file
        FileWriter fileStream = new FileWriter(outputFile);
        try {

            BufferedReader br = new BufferedReader(new FileReader(new File(
                    "idx.txt")));

            while ((line = br.readLine()) != null) {
                writer.write(line + "\n");
            }
            try {
                br.close();

            } catch (FileNotFoundException e) {
                e.getMessage();
            } catch (IOException e) {
                e.printStackTrace();
            }           BufferedWriter writer = new BufferedWriter(fileStream);

However that writes to the file as a human readable string. I need it to be written as binary data. How does one go about doing this in Java?

Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
Zeeno
  • 2,671
  • 9
  • 37
  • 60
  • What is your motivation behind? – TheOneTeam Aug 08 '11 at 11:43
  • 1
    Show some code that what have you done so far so I can help you on it – Talha Ahmed Khan Aug 08 '11 at 11:43
  • @ Kit Ho I was told by my boss the motivation behind it is to save file space so instead of writing multiple ascii characters to a file which increases the filesize, it can be written in binary to save space. The file will be sent over the internet and the data used to generate a 3d model in webgl. We want the model to load as fast as possible – Zeeno Aug 08 '11 at 11:48
  • @Talha Ahmed Khan I just updated it with some code – Zeeno Aug 08 '11 at 11:57
  • I guess you got your answers please tell me if that is not you want – Talha Ahmed Khan Aug 08 '11 at 11:59
  • Please make sure of one thing You are using `FileWriter` which will write the TEXT, You have to use `DataOutputStream` and `FileOutputStream` instead. They will write binary date. – Talha Ahmed Khan Aug 08 '11 at 12:04

2 Answers2

39

Maybe this fragment will help.

 int i = 42;
 DataOutputStream os = new DataOutputStream(new FileOutputStream("C:\\binout.dat"));
 os.writeInt(i);
 os.close();
Talha Ahmed Khan
  • 15,043
  • 10
  • 42
  • 49
rajah9
  • 11,645
  • 5
  • 44
  • 57
  • 4
    if you only want to write 2 byte use `writeShort()` – ratchet freak Aug 08 '11 at 12:01
  • 3
    @Ratchet freak The Java short ranges from -32,768 to 32,767 (according to http://download.oracle.com/javase/tutorial/java/nutsandbolts/datatypes.html). If the OP uses Java short (which is a great idea, thank you) he should subtract 32,768 before writing and add 32,768 after reading. – rajah9 Aug 08 '11 at 13:13
  • @rajah9 thanks! Such great ideas! This will reduce a decent amount of filesize. Thanks guys, I appreciate all the help :-) – Zeeno Aug 08 '11 at 14:46
  • 2
    Thumbs up for the number 42 :) – Dexter Apr 25 '16 at 10:50
2

What about the DataOutputStream. You can write int which contains 2 of your data integers.

DataOutputStream dos = new DataOutputStream(new FileOutputStream(<path>));
ArrayList<Integer> list = new ArrayList<Integer>();
int sum;
for( int i = 0; i < list.size(); i++ ) {
    if(i%2!=0){
        sum |= list.get( i ).intValue()<<16;
        dos.writeInt( sum );
    } else {
        sum = list.get( i ).intValue();
    }
}
oliholz
  • 7,447
  • 2
  • 43
  • 82