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






Read file upside/down with RandomAccessFile

 

import java.io.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 = fc.map(FileChannel.MapMode.READ_WRITE, 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);
    }
    fc.close();
  }
}

   
  








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.