简体   繁体   中英

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];

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.

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 ie start reading it into two dimensional array of java.You'll get the required output.

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