FileChannel: size() : FileChannel « java.nio.channels « Java by API






FileChannel: size()

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

public class MainClass {
  public static void main(String args[]) {
    FileInputStream fIn;
    FileChannel fChan;
    long fSize;
    ByteBuffer mBuf;

    try {
      fIn = new FileInputStream("test.txt");

      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);
    }
  }
}

           
         
    
    
  








Related examples in the same category

1.FileChannel.MapMode.READ_ONLY
2.FileChannel.MapMode.READ_WRITE
3.FileChannel: close()
4.FileChannel: lock(long position, long size, boolean shared)
5.FileChannel: map(MapMode mode, long position, long size)
6.FileChannel: position()
7.FileChannel: position(long newPosition)
8.FileChannel: read(ByteBuffer dst)
9.FileChannel: transferFrom(ReadableByteChannel src, long position, long count)
10.FileChannel: transferTo(long position, long count, WritableByteChannel target)
11.FileChannel: tryLock()
12.FileChannel: write(MappedByteBuffer buffer)
13.FileChannel: write(ByteBuffer buffer)