Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to output binary data to a file in Java?

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?

like image 708
Zeeno Avatar asked Aug 08 '11 11:08

Zeeno


People also ask

What is binary file in Java?

A binary file is a file whose content is in a binary format consisting of a series of sequential bytes, each of which is eight bits in length. The content must be interpreted by a program or a hardware processor that understands in advance exactly how that content is formatted and how to read the data.

Is Java file a binary file?

Java binary files are platform independent. They can be interpreted by any computer that supports Java. A stream is a device for transmitting or retrieving 8-bit or byte values. The emphasis is on the action of reading or writing as opposed to the data itself.

How does Java handle binary data?

A binary literal is a number that is represented in 0s and 1s (binary digits). Java allows you to express integral types (byte, short, int, and long) in a binary number system. To specify a binary literal, add the prefix 0b or 0B to the integral value.


2 Answers

Maybe this fragment will help.

 int i = 42;
 DataOutputStream os = new DataOutputStream(new FileOutputStream("C:\\binout.dat"));
 os.writeInt(i);
 os.close();
like image 155
rajah9 Avatar answered Oct 18 '22 02:10

rajah9


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();
    }
}
like image 41
oliholz Avatar answered Oct 18 '22 02:10

oliholz