java nio copy file

2014-11-24 08:22:20 · 作者: · 浏览: 0
[java]
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;


public class Copy {
public static void main(String [ ] args) throws IOException {

RandomAccessFile fromFile = new RandomAccessFile("fromFile.txt", "rw");
FileChannel fromChannel = fromFile.getChannel();

RandomAccessFile toFile = new RandomAccessFile("toFile.txt", "rw");
FileChannel toChannel = toFile.getChannel();

long position = 0; www.2cto.com
long count = fromChannel.size();

fromChannel.transferTo(position, count,toChannel);

}
}
作者:rjgcx2