简体   繁体   中英

Write binary file in Java

I have been facing quite some issue in translating a working MATLAB snippet into the respective JAVA looking one.

In MATLAB it works as follows:

fid = fopen('test.wnd','w');
fwrite(fid, -99, 'int16');
fwrite(fid, 7, 'int16');
fwrite(fid, 92, 'int32');
fwrite(fid, 3, 'int32');
fwrite(fid, 75.0, 'float32');
fwrite(fid, 75.0, 'float32');
fwrite(fid, 3.0, 'float32');
fwrite(fid, 2048, 'int32');
fwrite(fid, 8.0, 'float32');
fwrite(fid, 0, 'float32');
fwrite(fid, 0, 'float32');
fwrite(fid, 340.2, 'float32');
fwrite(fid, 0, 'int32');
fwrite(fid, 12, 'int32');
fwrite(fid, 3, 'int32');
fwrite(fid, 3, 'int32');
fwrite(fid, 0, 'float32');
fwrite(fid, 0, 'float32');
fwrite(fid, 113.4, 'float32');
fwrite(fid, 0, 'float32');
fwrite(fid, 0, 'float32');
fwrite(fid, 27.72, 'float32');
fwrite(fid, 12, 'float32');
fwrite(fid, 340.2, 'float32');

Scale = [1.0 .8 .5];
Offset = [8.0 0 0];

v = zeros(3*3,1);

for it = 1:nt
    cnt = 1;
    for iz = 1:nz
        for iy = y_ix
            for k=1:nffc
                v(cnt) =  ((velocity(it,k,iy,iz) - Offset(k))/Scale(k)*1000);
                cnt = cnt + 1;                
            end %for k
        end %iy
    end % iz 

    fwrite( fid_wnd, v, 'int16' );

end %it
fclose(fid);

Therefore, in JAVA , I tried this:

DataOutputStream dos = new DataOutputStream(new FileOutputStream("C:/Users/fperrone/Desktop/test.wnd"));
dos.writeShort(-99);
dos.writeShort(7);
dos.writeInt(92);
dos.writeInt(3);        
dos.writeFloat((float)75.0);
dos.writeFloat((float)75.0);
dos.writeFloat((float)2.4);
dos.writeInt(2048);
dos.writeFloat((float)8.0);
dos.writeFloat((float)0);
dos.writeFloat((float)0);
dos.writeFloat((float)340.2);
dos.writeInt(0);
dos.writeInt(67);
dos.writeInt(3);
dos.writeInt(3);
dos.writeFloat((float)0);
dos.writeFloat((float)0);
dos.writeFloat((float)113.4);
dos.writeFloat((float)0);
dos.writeFloat((float)0);
dos.writeFloat((float)27.72);
dos.writeFloat((float)12);
dos.writeFloat((float)340.2);
double[] Offset = {8.0, 0.0, 0.0};
double[] Scale = {1.0, .8, .5};
short v[] = new short[9];
        for(int it = 0; it < 4096; ++it){
            for(int comp = 0; comp < 3; ++comp){
                int count = 0;
                for(int iy = 0; iy < 3; ++iy){
                    for(int iz = 0; iz < 3; ++iz){
                        v[count] = (short) ((test[it][comp][iy][iz] - Offset[comp])/Scale[comp]*1000);
                        dos.writeShort(v[count]);
                        count += 1;
                    }
                }
            }           
        }       
        dos.close();

The point is that when I link the JAVA output with a software the binary file is meant for, this software does not read any information, wheres the MATLAB output is read perfectly as expected.

What am I missing?

Since you haven't specified the "machine format" in either fopen() or fwrite() in the matlab program, it will use the 'native' system byte ordering by default.

You need to specify your binary format while creating a file in Matlab using the "machineformat" field.

fileID = fopen(filename,permission,machinefmt,encodingIn) fwrite(fileID, A, precision, skip, machineformat)

More information here: http://www.mathworks.com/help/matlab/ref/fopen.html http://www.mathworks.com/help/matlab/ref/fwrite.html

For correct read from a java program, you need to do this: fid = fopen('test.wnd','w', 's');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM