Want to save the number -9999 into a binary file

Hello:

I have this function in which I save different numbers (integers or doubles) into a binary file (with another extension) but when i tried to convert -9999 into bytes and then save it in the file ; it saves -9960 not -9999 as it should. I do not know why.

This is my function:

/*********************************************************************/

public static void createFile(Double [][] Grid, String txtOutputFile, String[] gridInfoFile){

Integer[][] intGrid = null ;

File file = new File(Utils.ChangeFileExt(txtOutputFile, ".myExt"));

try {

FileOutputStream file_out = new FileOutputStream(file);

DataOutputStream data_out = new DataOutputStream(file_out);

if(gridInfoFile[0].equalsIgnoreCase("1") || gridInfoFile[0].equalsIgnoreCase("1.0")){

intGrid = doubleToIntArray(Grid);

}

for(int i = 0; i < Grid.length; i++){

for(int j = 0; j < Grid[0].length; j++){

if(gridInfoFile[0].equalsIgnoreCase("1") || gridInfoFile[0].equalsIgnoreCase("1.0")){

Byte byte1, byte2;

byte1 = new Integer (intGrid[j] / 256).byteValue();

byte2 = new Integer(intGrid[j] - (intGrid[j] / 256)).byteValue();

data_out.write(byte2);

data_out.write(byte1);

}

if(gridInfoFile[0].equalsIgnoreCase("2") || gridInfoFile[0].equalsIgnoreCase("2.0")){

ByteArrayOutputStream byte_out = new ByteArrayOutputStream ();

DataOutputStream data_out2 = new DataOutputStream (byte_out);

data_out2.writeFloat(Grid[j].floatValue());

byte[] bArray = byte_out.toByteArray();

data_out.write(bArray[3]);

data_out.write(bArray[2]);

data_out.write(bArray[1]);

data_out.write(bArray[0]);

}

}

}//fin for

data_out.close();

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

/********************************************/

I had already tried data_out.writeFloat, write, writeInt, etc without any success.

If someone knows the answer , please let me know, will appreciate that. Thank you for your help

magnasound

[2227 byte] By [mac_magnasounda] at [2007-11-27 2:30:18]
# 1
Since you are using DataOutputStream, why not use some of its more helpful methods, like writeInt?
DrLaszloJamfa at 2007-7-12 2:43:56 > top of Java-index,Java Essentials,Java Programming...