Split and join back a binary file in java

后端 未结 7 830
闹比i
闹比i 2020-12-13 10:45

I am trying to divide a binary file (like video/audio/image) into chunks of 100kb each and then join those chunks back to get back the original file. My code seems to be wor

7条回答
  •  旧巷少年郎
    2020-12-13 11:09

    I can spot only 2 potential mistakes in the code:

    int fileSize = (int) ifile.length();
    

    The above fails when the file is over 2GB since an int cannot hold more.

    newName = fname + ".part" + Integer.toString(nChunks - 1);
    

    A filename which is constructed like that should be sorted on a very specific manner. When using default string sorting, name.part10 will namely come before name.part2. You'd like to supply a custom Comparator which extracts and parses the part number as an int and then compare by that instead.

提交回复
热议问题