import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.channels.FileChannel;
import java.nio.file.Files;
import org.apache.commons.io.FileUtils;
/**
小编推荐一个学JAVA的学习裙【 一三三,九三零,六九三】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!* 复制文件的四种方法
* @author
*
*/
public class IOCopyfile {
/**
* java.io.FileInputStream;
* @param source
* @param dest
* @throws IOException
*/
private static void copyfileusefilestream(File source,File dest) throws IOException {
FileInputStream in=null;
FileOutputStream out=null;
try {
in=new FileInputStream(source);
out=new FileOutputStream(dest);
/*BufferedInputStream bi=new BufferedInputStream(in);//处理输入流-缓冲流 套接文件流
BufferedOutputStream bo=new BufferedOutputStream(out);//处理输出流-缓冲流*/
byte[] buff=new byte[1024];//输入流前缓存容器
int read=in.read(buff);//输入流读缓存中的
while(read>0) {
out.write(buff, 0, read);//输出流 写
}
小编推荐一个学JAVA的学习裙【 一三三,九三零,六九三】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!}finally {
out.close();
in.close();
}
}
/**
* java.nio.channels.FileChannel;
* @param source
* @param dest
* @throws IOException
*/
private static void copyfileusefilechannels(File source,File dest) throws IOException {
FileChannel in=null;
FileChannel out=null;
try {
in=new FileInputStream(source).getChannel();
out=new FileInputStream(dest).getChannel();
out.transferFrom(in, 0, in.size());
} finally {
out.close();
in.close();
}
}
/**
* java.nio.file.Files;
* @param source
* @param dest
* @throws IOException
*/
private static void copyfileusejava7(File source,File dest) throws IOException {
Files.copy(source.toPath(), dest.toPath());
}
/**导入commons-io-xx.jar
* 使用org.apache.commons.io.FileUtils;基于java.nio.channels.FileChannel
* @param source
* @param dest
* @throws IOException
*/
private static void copyfileusecommonsIO(File source,File dest) throws IOException {
FileUtils.copyFile(source, dest);
}
/**
* 测试
* @param args
* @throws IOException
*/
public static void main(String[] args) throws IOException {
File srcFile=new File("c:\");
File desFile=new File("d:\");
System.out.println(System.currentTimeMillis()/*System.nanoTime()*/);//复制文件开始时间
IOCopyfile.copyfileusefilestream(srcFile, desFile);
/*IOCopyfile.copyfileusefilechannels(srcFile, desFile);
IOCopyfile.copyfileusejava7(srcFile, desFile);
IOCopyfile.copyfileusecommonsIO(srcFile, desFile);*/
System.out.println(System.currentTimeMillis()/*System.nanoTime()*/);//复制文件结束时间
}小编推荐一个学JAVA的学习裙【 一三三,九三零,六九三】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!
}