Copy a file with Java nio 

Joined:
08/13/2009
Posts:
172

October 10, 2013 12:48:19    Last update: October 10, 2013 12:48:19
Sample code to copy a file with Java nio.
import java.io.*;
import java.nio.channels.FileChannel;

public class CopyFile {
    public static void main(String[] args) throws Exception {
	if (args.length != 2) {
	    System.out.println("Usage: java CopyFile <src> <dest>");
	    return;
	}

	FileChannel src = new FileInputStream(args[0]).getChannel();
	FileChannel dest = new FileOutputStream(args[1]).getChannel();
	dest.transferFrom(src, 0, src.size());

	src.close();
	dest.close();
    }
}
Share |
| Comment  | Tags