I have a java class file with three arrayLists, one with type String, one with type Integer and other is ArrayList with type (ArrayList(String)). I have to write these these arraylists to a structure in C with character arrays, integers and short and output a file in a specefic format extension. The file has to be readable again by the same application. What is the best way to trasnfer the data from java to c structure and then output the c structure in a file. Thank you
-
There is no best way. You just have to choose format of file like CSV, XML, JSON, your own format, that can be read on both sides (Java,C) – Piro Sep 04 '13 at 05:51
-
I have tried understanding google protocol buffers but I dont know how to go about it and by best way i meant, how should i go about it. I have to write the java structures to a c compatible file, and lator on read the c compatible file to be loaded in java application – Asim Mushtaq Sep 04 '13 at 05:55
-
What does "C compatible file" mean? – hyde Sep 04 '13 at 06:03
-
Yes using JNI would be more appropriate. I have converted an application from C to Java, but the file previously written by C from C structures is now to be written by java application and because the application interfaces with hardware, it only understands data written in C data types and memory allocation. so now my application in java needs to somehow convert java type variables to c type variables with similar memory allocations to be understandable by the interfacing hardware – Asim Mushtaq Sep 04 '13 at 06:03
2 Answers
There is no "C compatible file" format. If you have C structs written to disk file directly, then those are in an ad-hoc binary format. Exact format depends on things like packing and padding of the struct, byte order, word size of the CPU (like, 32 or 64 bit), etc.
So, start by defining the format, then forget it is produced by C.
Once you have the format defined, you can write a program to parse it in Java. If it is short with fixed length records, I'd probably create a class, which internally has just a private byte[]
array, and then methods to manipulate it, save it and load it.
-
That makes more sense to me now. Thank you hyde. I will try defining the structure and reading and writing the file and get back. – Asim Mushtaq Sep 04 '13 at 06:27
I suggest you write/read the data to a ByteBuffer using native byte ordering. The rest is up to you are to how you do it.
A library which might help is Javolution's Struct library which helps you map C structs onto ByteBuffers. This can help with C's various padding rules i.e.the exact layout might not be obvious.

- 525,659
- 79
- 751
- 1,130
-
This one seems more relavant. Thank you Peter, I ll try this one and get back lator on. Is there a way to include a .c file in your java project, and call a c function from that .java class file – Asim Mushtaq Sep 04 '13 at 06:11
-
@techXploiter Yes and no. You can include it but not in a way which avoids you having to do the translation from Java to C. Once you have translated it you could use it in native code, but that won't really help you. – Peter Lawrey Sep 04 '13 at 06:17
-
That means its better to do the translation write the file and read it the other way round without including the native file – Asim Mushtaq Sep 04 '13 at 06:30
-
@techXploiter most likely. You might be able to write a parser to generate the Structs. – Peter Lawrey Sep 04 '13 at 07:13