dilix
dilix

Reputation: 3893

Read int[][] from file. Java

There is one file that was created with Objective C. In this file there is an array: int[x][y]. There is no problem to read this file 'as is' in C and pass only link of the new int[][] as buffer.

But I want to read this file in Java, I've tried read it to int[x*y] but values are different because C stores a double array ([][]) not like simple [].

I think the are 2 ways: recreate file for usage in Java or use C++ in java, but maybe there are some easier ways?

UPDATED

Creation of the file (inf Objective C) (the size of array is fixed each time)

NSData *mydata = [NSData dataWithBytes:&areas length:sizeof(areas)];
[mydata writeToFile:filePathData options:NSDataWritingAtomic error:&error];

Upvotes: 0

Views: 249

Answers (2)

perilbrain
perilbrain

Reputation: 8207

There is concept of Multi-dimensional arrays in Java as well.

Just copy the writer algorithm of C and translate it into java in reverse order i.e. start reading it into two dimensional array of java.You'll get the required output.

Upvotes: 0

Peter Lawrey
Peter Lawrey

Reputation: 533560

I imagine the problem is you are reading little endian C data as big endian. If you use DataInputStream it assumes big endian which is only the default for C on big endian machines such as Sparc ;) and a number of machines now all but dead.

I assume you know the format of the file and if it doesn't contain the dimensions (width and height), you know what they are.

I suggest you try using ByteBuffer with order(ByteOrder.LITTE_ENDIAN) to read the data. There are a number of ways to do this. If you file is smaller than 2 GB I would use a memory mapped file.

Upvotes: 3

Related Questions