Search code examples
javarandomaccessfile

Java transferFrom Part of binary file?


Im tring to transfer from a file filein starting at position 1300 (in uint8 pieces) into fileto, using the RandomAccessFile transferFrom function.

fromfile = java.io.RandomAccessFile(ifile, 'rw');
fromchannel = fromfile.getChannel();
tofile = java.io.RandomAccessFile(ofile, 'rw');
tochannel = tofile.getChannel();
tochannel.transferFrom(fromchannel,n,fromfile.length()-n)

tochannel.close();
fromchannel.close();
fromfile.close();
tofile.close();

My output file is just empty tho.

Anyone know what im doing wrong??

Edit 1:

I've changed

tochannel.transferFrom(fromchannel,n,fromfile.length()-n)

to

fromchannel.transferTo(n,fromfile.length()-n,tochannel)

But now the output is printing to the all the file right except for it puts alot of 00 hexadecimals where the header in the original was???


Solution

  • You want to use transferTo I believe

    fromchannel.transferTo(n,fromfile.length()-n,tochannel)
    

    as transferFrom tries to start at position n in the outfile, while transferTo will start at position n in the infile