FileChannel

In this chapter you will learn:

  1. How to use FileChannel
  2. How to create a FileChannel for reading and writing
  3. How to get the size of a FileChannel

Use FileChannel

A channel for reading, writing, mapping, and manipulating a file.

A file channel has a current position within its file which can be both queried and modified. File channels are safe for use by multiple concurrent threads.

FileChannel.MapMode.READ_ONLY defines a mode for a read-only mapping.

import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
/*from  j a va2  s. c  o  m*/
public class MainClass {

  public static void main(String args[]) {
    FileInputStream fIn;
    FileOutputStream fOut;
    FileChannel fIChan, fOChan;
    long fSize;
    MappedByteBuffer mBuf;

    try {
      fIn = new FileInputStream(args[0]);
      fOut = new FileOutputStream(args[1]);

      fIChan = fIn.getChannel();
      fOChan = fOut.getChannel();

      fSize = fIChan.size();

      mBuf = fIChan.map(FileChannel.MapMode.READ_ONLY, 0, fSize);

      fOChan.write(mBuf); // this copies the file

      fIChan.close();
      fIn.close();

      fOChan.close();
      fOut.close();
    } catch (IOException exc) {
      System.out.println(exc);
      System.exit(1);
    } catch (ArrayIndexOutOfBoundsException exc) {
      System.out.println("Usage: Copy from to");
      System.exit(1);
    }
  }
}

Create a FileChannel for reading and writing

FileChannel.MapMode.READ_WRITE defines the mode for a read/write mapping.

import java.io.File;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.IntBuffer;
import java.nio.channels.FileChannel;
//from  ja  va  2s . c  o  m
public class Main {
  public static void main(String[] args) throws IOException {
    FileChannel fc = new RandomAccessFile(new File("temp.tmp"), "rw").getChannel();
    IntBuffer ib = fc.map(FileChannel.MapMode.READ_WRITE, 0, fc.size()).asIntBuffer();
    ib.put(0);
    for (int i = 1; i < 10; i++)
      ib.put(ib.get(i - 1));
    fc.close();

  }
}

Get the size of a FileChannel

abstract long size() returns the current size of this channel's file.

import java.io.FileInputStream;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;
//ja  v  a 2 s.c o m
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);
    }
  }
}

Next chapter...

What you will learn in the next chapter:

  1. How to get FileChannel position for reading and writing
  2. How to set new position for a FileChannel
Home » Java Tutorial » NIO
NIO
FileChannel
FileChannel position
FileChannel lock
FileChannel read
FileChannel write