//把一个较大的文件(G以上)复制到其它文件夹中
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.MappedByteBuffer;
import java.nio.channels.FileChannel;
import java.nio.channels.FileChannel.MapMode;
public class CopyBigFile {
public static void main(String[] args) {
// TODO Auto-generated method stub
//声明流,不初始化
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
//初始化流
//读取大文件,第一个参数是文件路径,第二个参数是读取权限
inChannel = new RandomAccessFile("e:\\1、Object类.wmv", "r").getChannel();
//写入大文件
outChannel = new RandomAccessFile("d:\\obj.wmv", "rw").getChannel();
//把内存映射到缓冲区
MappedByteBuffer map = inChannel.map(MapMode.READ_ONLY, 0, inChannel.size());
//把缓冲区的内容通过输出管道写到指定路径中(并重新命名)
outChannel.write(map);
System.out.println("读取结束!");
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} finally {
//通过判断关闭通道
try {
//通道不等于空说明通道被打开了,防止空指针异常
if(outChannel!=null) {
outChannel.close();
}
if(inChannel!=null) {
inChannel.close();
}
}catch(IOException e) {
e.printStackTrace();
}
}
}
}