I am trying to read an array of ints from a RandomAccessFile. RandomAccessFile however only supports reading for an array of bytes. My code so far:
public long getSumOfElementsFromArray(long start, int length)
{
int[] tempArray = new int[length];
try
{
RAF.seek(start);
RAF.readFully( (byte[]) (tempArray) , 0, length*4);
//do some stuff with tempArray
}
catch(IOException e)
{
e.printStackTrace();
}
return 0;
}
Eclipse tells me: "Cannot cast from int[] to byte[]". In C I could easily cast int* to char* but I do not know how this is done in Java. How could I do this in Java?
You can use ByteBuffer
. Read as a byteArray
and then convert.
int[] tempArray = ByteBuffer.wrap(byteArray).asIntBuffer().array();
Check similar question.