Mega Code Archive

 
Categories / Java Tutorial / File
 

Get MappedByteBuffer from FileChannel

import java.io.RandomAccessFile; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MainClass {   public static void main(String[] argv) throws Exception {     RandomAccessFile raf = new RandomAccessFile("test.txt", "r");     FileChannel fc = raf.getChannel();     MappedByteBuffer buffer = fc.map(FileChannel.MapMode.READ_ONLY, 0, fc.size());     buffer.clear();     buffer.flip();     System.out.println("hasArray=" + buffer.hasArray());     System.out.println(buffer.toString());     System.out.flush();   } }