NIO通过transferFrom或者transferTo来复制文件:
public static void main(String[] args) throws Exception { //输入管道 FileInputStream fileInputStream = new FileInputStream("E:\\file\\1.jpg"); FileChannel inChannel = fileInputStream.getChannel(); //输出管道 FileOutputStream fileOutputStream = new FileOutputStream("E:\\file\\4.jpg"); FileChannel outChannel = fileOutputStream.getChannel(); //表示从文件信息从哪来来的 1、输入管道信息,2、起始位置信息,3、输入的最大值 //outChannel.transferFrom(inChannel,0,inChannel.size()); //也可以通过文件信息到哪里去来设置 1、起始位置信息,2、输入的最大值,3、输出管道信息 inChannel.transferTo(0,inChannel.size(),outChannel); System.out.println("设置成功"); //关闭管道和缓冲区 outChannel.close(); fileOutputStream.close(); inChannel.close(); fileInputStream.close(); } |