How to output binary data to a file in Java?
Asked Answered
A

2

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?

Aubergine answered 8/8, 2011 at 11:40 Comment(6)
What is your motivation behind?Agentive
Show some code that what have you done so far so I can help you on itMagical
@ 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 possibleAubergine
@Talha Ahmed Khan I just updated it with some codeAubergine
I guess you got your answers please tell me if that is not you wantMagical
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.Magical
B
39

Maybe this fragment will help.

 int i = 42;
 DataOutputStream os = new DataOutputStream(new FileOutputStream("C:\\binout.dat"));
 os.writeInt(i);
 os.close();
Boreas answered 8/8, 2011 at 11:47 Comment(4)
if you only want to write 2 byte use writeShort()Tocopherol
@Ratchet freak The Java short ranges from -32,768 to 32,767 (according to download.oracle.com/javase/tutorial/java/nutsandbolts/…). 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.Boreas
@Boreas thanks! Such great ideas! This will reduce a decent amount of filesize. Thanks guys, I appreciate all the help :-)Aubergine
Thumbs up for the number 42 :)Dilettantism
D
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();
    }
}
Doelling answered 8/8, 2011 at 11:50 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.