Java – Convert files to byte arrays in reverse order

Convert files to byte arrays in reverse order… here is a solution to the problem.

Convert files to byte arrays in reverse order

byte[] bFile = new byte[(int) file.length()];

FileInputStream fileInputStream = new FileInputStream(file);
fileInputStream.read(bFile);
fileInputStream.close();

This code lets me convert the file to a byte array, and I’m looking to read the file from start to finish (in reverse order).

EDIT: I don’t want to read the whole file. The last part (example is about 1000 bytes).

Solution

File file = new File(/*file path*/);
byte[] bFile = new byte[1000];

RandomAccessFile fileInputStream = new RandomAccessFile(file, "r");
fileInputStream.seek(fileInputStream.length() - bFile[0].length);
fileInputStream.read(bFile, 0, bFile.length);
fileInputStream.close();

I just figured it out, reading the last 1000 bytes of the file

Related Problems and Solutions