Java text file read by FileChannel

Description

Java text file read by FileChannel

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

public class Main {
   public static void main(String args[]) {
      FileInputStream fIn;/*from ww w .  ja v  a  2s.c o  m*/
      FileChannel fChan;
      long fSize;
      ByteBuffer mBuf;

      try {
         fIn = new FileInputStream("Main.java");
         fChan = fIn.getChannel();
         fSize = fChan.size();
         mBuf = ByteBuffer.allocate((int) fSize);
         fChan.read(mBuf);
         mBuf.rewind();
         for (int i = 0; i < fSize; i++)
            System.out.print((char) mBuf.get());
         fChan.close();
         fIn.close();
      } catch (IOException exc) {
         System.out.println(exc);
         System.exit(1);
      }
   }
}



PreviousNext

Related