Copy file via file Channel - Android java.nio.channels

Android examples for java.nio.channels:FileChannel

Description

Copy file via file Channel

Demo Code

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.channels.FileChannel;

public class Main {

  public static void fileChannelCopy(File s, File t) {
    FileInputStream fi = null;/*from  w  ww  .  ja  va2s  .  c  o  m*/
    FileOutputStream fo = null;
    try {
      fi = new FileInputStream(s);
      fo = new FileOutputStream(t);
      FileChannel in = fi.getChannel();
      FileChannel out = fo.getChannel();
      in.transferTo(0, in.size(), out);
    } catch (IOException e) {
      e.printStackTrace();
    } finally {
      try {
        if (fo != null)
          fo.close();
        if (fi != null)
          fi.close();
      } catch (IOException e) {
        e.printStackTrace();
      }

    }
  }

}

Related Tutorials