Best way to read structured binary files with Java

前端 未结 12 953
我寻月下人不归
我寻月下人不归 2020-12-04 13:23

I have to read a binary file in a legacy format with Java.

In a nutshell the file has a header consisting of several integers, bytes and fixed-length char arrays, f

12条回答
  •  情书的邮戳
    2020-12-04 13:54

    As other people mention DataInputStream and Buffers are probably the low-level API's you are after for dealing with binary data in java.

    However you probably want something like Construct (wiki page has good examples too: http://en.wikipedia.org/wiki/Construct_(python_library), but for Java.

    I don't know of any (Java versions) off hand, but taking that approach (declaratively specifying the struct in code) would probably be the right way to go. With a suitable fluent interface in Java it would probably be quite similar to a DSL.

    EDIT: bit of googling reveals this:

    http://javolution.org/api/javolution/io/Struct.html

    Which might be the kind of thing you are looking for. I have no idea whether it works or is any good, but it looks like a sensible place to start.

提交回复
热议问题