Read file upside/down with RandomAccessFile : MappedByteBuffer « File Input Output « Java

Read file upside/down with RandomAccessFile


import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
  public static void main(String[] args) throws Exception {
    RandomAccessFile f = new RandomAccessFile("hello.txt", "rw");
    FileChannel fc = f.getChannel();
    MappedByteBuffer mbb =, 0, f.length());

    int len = (int) f.length();
    for (int i = 0, j = len - 1; i < j; i++, j--) {
      byte b = mbb.get(i);
      mbb.put(i, mbb.get(j));
      mbb.put(j, b);


Related examples in the same category

1.Persisting Changes to a Memory-Mapped ByteBuffer
2.Map FileChannel to MappedByteBuffer
3.Use a mapped file to read a text file.