Mega Code Archive

 
Categories / Java / File Input Output
 

FileChannel

import java.io.FileInputStream; import java.io.IOException; import java.nio.MappedByteBuffer; import java.nio.channels.FileChannel; public class MainClass {   public static void main(String args[]) {     FileInputStream fileInputStream;     FileChannel fileChannel;     long fileSize;     MappedByteBuffer mBuf;     try {       fileInputStream = new FileInputStream("test.txt");       fileChannel = fileInputStream.getChannel();       fileSize = fileChannel.size();       mBuf = fileChannel.map(FileChannel.MapMode.READ_ONLY, 0, fileSize);       for (int i = 0; i < fileSize; i++)         System.out.print((char) mBuf.get());       fileChannel.close();       fileInputStream.close();     } catch (IOException exc) {       System.out.println(exc);       System.exit(1);     }   } }