Java examples for File Path IO:File Channel
Copying Files with FileChannel and a Direct ByteBuffer
import java.io.IOException; import java.nio.ByteBuffer; import java.nio.channels.FileChannel; import java.nio.file.Path; import java.nio.file.Paths; import java.nio.file.StandardOpenOption; import java.util.EnumSet; public class Main { public static void main(String[] args) { Path copy_from = Paths.get("C:/folder1/folder2/folder3/videos/test.mp4"); Path copy_to = Paths.get("C:/test.mp4"); int bufferSizeKB = 4; int bufferSize = bufferSizeKB * 1024; System.out.println("Using FileChannel and direct buffer ..."); try (FileChannel fileChannel_from = (FileChannel.open(copy_from, EnumSet.of(StandardOpenOption.READ))); FileChannel fileChannel_to = (FileChannel .open(copy_to, EnumSet.of(StandardOpenOption.CREATE_NEW, StandardOpenOption.WRITE)))) { ByteBuffer bytebuffer = ByteBuffer.allocateDirect(bufferSize); int bytesCount; while ((bytesCount = fileChannel_from.read(bytebuffer)) > 0) { bytebuffer.flip();//from w ww. jav a 2s . co m fileChannel_to.write(bytebuffer); bytebuffer.clear(); } } catch (IOException ex) { System.err.println(ex); } } }