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

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

This code let's me convert file to byte array, am looking for reading file from end to start (in reverse order)

Edit : i dont wan't to read entire file. A part at the end (Example around 1000 bytes)

8
  • 1
    Why don't you just use the data in the byte array in reverse order? Since it's in an array you're free to do as you please with it. Commented Jun 1, 2016 at 21:18
  • yeah but i just need only a part of file length at the end Commented Jun 1, 2016 at 21:19
  • Ah... See, that's important information. What you are asking is if there is some way to read the data at the end only and not the beginning also? Commented Jun 1, 2016 at 21:20
  • reading whole file and reversing the byte array will take soo much memory , may even throw out of memory exception. I need to perform this task for many files Commented Jun 1, 2016 at 21:21
  • @blahfunk exactly, sorry for incomplete question.. I need a read a part of file at the end (may be around 1000 byte) Commented Jun 1, 2016 at 21:23

1 Answer 1

3
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 last 1000 bytes of a file

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.