java 文件拷贝方法_JAVA之道丨文件复制的四种方法

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;

/**

8bd01b575c89

小编推荐一个学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);//输出流 写

}

8bd01b575c89

小编推荐一个学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的学习裙【 一三三,九三零,六九三】,无论你是大牛还是小白,是想转行还是想入行都可以来了解一起进步一起学习!裙内有开发工具,很多干货和技术资料分享!

}

8bd01b575c89

8bd01b575c89

8bd01b575c89

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值