一:缓冲输入输出流(InputStream、OutputStream)
- <span style="font-size:14px;">
-
-
- public static boolean copyFile(String srcFileName,String destFileName,boolean overlay){
- File srcFile = new File(srcFileName);
-
- if(!srcFile.exists()){
- try{
- throw new Exception("源文件:"+srcFileName+"不存在!");
- }catch(Exception e){
- e.printStackTrace();
- }
- return false;
- }else if(!srcFile.isFile()){
- try {
- throw new Exception("复制文件失败,源文件:"+srcFileName+"不是一个文件!");
- } catch (Exception e) {
-
- e.printStackTrace();
- }
- return false;
-
- }
-
- File destFile = new File(destFileName);
- if(destFile.exists()){
-
- if(overlay){
-
- new File(destFileName).delete();
-
- }
- }else{
-
- if(!destFile.getParentFile().exists()){
-
-
- if(!destFile.getParentFile().mkdirs()){
-
- return false;
- }
- }
- }
-
-
- int byteread = 0;
- InputStream in = null;
- OutputStream out = null;
-
- try {
- in = new FileInputStream(srcFile);
- out = new FileOutputStream(destFile);
- byte[] buffer = new byte[1024];
-
-
-
-
-
-
-
-
-
-
- while((byteread = in.read(buffer))!=-1){
- out.write(buffer, 0, byteread);
- }
- return true;
- } catch (FileNotFoundException e) {
-
- return false;
- } catch (IOException e) {
- return false;
- }finally{
- try {
- if(out!=null){
- out.close();
- }
- if(in!=null){
- in.close();
- }
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
- }</span>
二:文件通道(FileChannel)
- <span style="font-size:14px;">
-
-
- public static void fileChannelCopy(String srcDirName,String destDirName){
- FileInputStream fi = null;
- FileOutputStream fo = null;
- FileChannel in = null;
- FileChannel out = null;
-
- try {
- fi = new FileInputStream(new File(srcDirName));
- fo = new FileOutputStream( new File(destDirName));
- in = fi.getChannel();
- out = fo.getChannel();
-
-
-
-
-
-
-
-
-
- in.transferTo(0, in.size(), out);
- } catch (FileNotFoundException e) {
-
- e.printStackTrace();
- } catch (IOException e) {
-
- e.printStackTrace();
- }finally{
- try {
- fi.close();
- in.close();
- fo.close();
- out.close();
- } catch (IOException e) {
-
- e.printStackTrace();
- }
- }
-
- }</span>
测试上面的两个方法:- <span style="font-size:14px;">public class copyFile{
- public static void main(String[] args) {
- String srcDirName = "E:/360cse_official.exe";
- String destDirName ="E:/360cse_official_test.exe";
- long start;
- long end;
- start = System.currentTimeMillis();
- copyFile.copyFile(srcDirName,destDirName,true);
- end = System.currentTimeMillis();
- System.out.println("缓冲输入输出流方式复制文件,用时:"+(end-start)+"ms");
-
- start = System.currentTimeMillis();
- copyFile.fileChannelCopy(srcDirName,destDirName);
- end = System.currentTimeMillis();
- System.out.println("使用文件通道的方式复制文件,用时:"+(end-start)+"ms");
-
- }</span>
测试文件:
运行结果: 缓冲输入输出流方式复制文件,用时:499ms
使用文件通道的方式复制文件,用时:57ms
总结:
通过对上面两个方法测试,我们可以知道使用文件通道的方式复制文件,明显比输出流复制文件效率要高。
学到的知识点:
①:返回系统的当前时间:start = System.currentTimeMillis();
②:write(byte[] b, int off, int len) 方法
将指定 byte 数组中从偏移量 off 开始的 len 个字节写入此输出流
b - 数据
off - 数据中的起始偏移量。
len - 要写入的字节数。
③:in.read(buffer))!=-1,是从流buffer中读取一个字节,当流结束的时候read返回-1
④:in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道中
public abstract long transferTo(long position, long count,
WritableByteChannel target)throws IOException;
position - 文件中的位置,从此位置开始传输;必须为非负数
count - 要传输的最大字节数;必须为非负数
target - 目标通道
返回:
实际已传输的字节数,可能为零
0 0