How to output binary data to a file in Java?

前端 未结 2 1281
广开言路
广开言路 2020-12-14 02:37

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

相关标签:
2条回答
  • 2020-12-14 03:08

    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();
        }
    }
    
    0 讨论(0)
  • 2020-12-14 03:22

    Maybe this fragment will help.

     int i = 42;
     DataOutputStream os = new DataOutputStream(new FileOutputStream("C:\\binout.dat"));
     os.writeInt(i);
     os.close();
    
    0 讨论(0)
提交回复
热议问题