Best way to read structured binary files with Java

前端 未结 12 944
我寻月下人不归
我寻月下人不归 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:55

    If you would be using Preon, then all you would have to do is this:

    public class Header {
        @BoundNumber int version;
        @BoundNumber byte type;
        @BoundNumber int beginOfData;
        @BoundString(size="15") String id;
    }
    

    Once you have this, you create Codec using a single line:

    Codec
    codec = Codecs.create(Header.class);

    And you use the Codec like this:

    Header header = Codecs.decode(codec, file);
    

提交回复
热议问题