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?

6
  • What is your motivation behind?
    – Kit Ho
    Commented Aug 8, 2011 at 11:43
  • 1
    Show some code that what have you done so far so I can help you on it Commented Aug 8, 2011 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
    Commented Aug 8, 2011 at 11:48
  • @Talha Ahmed Khan I just updated it with some code
    – Zeeno
    Commented Aug 8, 2011 at 11:57
  • I guess you got your answers please tell me if that is not you want Commented Aug 8, 2011 at 11:59

2 Answers 2

39

Maybe this fragment will help.

 int i = 42;
 DataOutputStream os = new DataOutputStream(new FileOutputStream("C:\\binout.dat"));
 os.writeInt(i);
 os.close();
4
  • 4
    if you only want to write 2 byte use writeShort() Commented Aug 8, 2011 at 12:01
  • 3
    @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.
    – rajah9
    Commented Aug 8, 2011 at 13:13
  • @rajah9 thanks! Such great ideas! This will reduce a decent amount of filesize. Thanks guys, I appreciate all the help :-)
    – Zeeno
    Commented Aug 8, 2011 at 14:46
  • 2
    Thumbs up for the number 42 :)
    – Dexter
    Commented Apr 25, 2016 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();
    }
}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.