文件拷贝

package com.timeschedule.timeschedule.test;

import java.io.*;
import java.nio.channels.FileChannel;

public class FileCopyMethods {

    /**
     * 文件输入输出流
     */

    public static void copyFile1(File f1,File f2) throws IOException {
        if (!f2.exists()){
            f2.mkdirs();
            f2.createNewFile();
        }
        f2.setWritable(true);
        long start = System.currentTimeMillis();
        InputStream in = null;
        OutputStream out = null;
        try{
            in= new BufferedInputStream(new FileInputStream(f1));
            out=new BufferedOutputStream(new FileOutputStream(f2));
            byte[] b=new byte[1024];
            int flag=0;
            while((flag=in.read(b))!=-1){
                out.write(b, 0, flag);
            }
        }catch (IOException ex){

        }finally {
            if (in!=null){
                in.close();
            }
            if (out!=null){
                out.close();
            }
            long end = System.currentTimeMillis();
            System.out.println((end-start)/1000+"秒,"+(end-start)+"毫秒");
        }
    }

    public static void  copyFile2(File f1,File f2) throws IOException {
        if (!f2.exists()){
            f2.mkdirs();
            f2.createNewFile();
        }
        f2.setWritable(true);
        long start = System.currentTimeMillis();
        FileInputStream fi = null;
        FileOutputStream fo = null;
        FileChannel in = null;
        FileChannel out = null;
        try {
            fi = new FileInputStream(f1);
            fo = new FileOutputStream(f2);
            in = fi.getChannel();//得到对应的文件通道
            out = fo.getChannel();//得到对应的文件通道
            in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                fi.close();
                in.close();
                fo.close();
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
            long end = System.currentTimeMillis();
            System.out.println((end-start)/1000+"秒,"+(end-start)+"毫秒");
        }
    }

    public static void copyFile3(File f1,File f2) throws IOException {
        if (!f2.exists()){
            f2.mkdirs();
            f2.createNewFile();
        }
        f2.setWritable(true);
        long start = System.currentTimeMillis();
        FileInputStream fileInputStream = null;
        FileOutputStream fileOutputStream = null;
        try{
            fileInputStream = new FileInputStream(f1);
            fileOutputStream =new FileOutputStream(f2);
            byte[] b=new byte[1024];
            int flag=0;
            while((flag=fileInputStream.read(b))!=-1){
                fileOutputStream.write(b, 0, flag);
            }
        }catch (IOException ex){

        }finally {
            if (fileInputStream!=null){
                fileInputStream.close();
            }
            if (fileOutputStream!=null){
                fileOutputStream.close();
            }
            long end = System.currentTimeMillis();
            System.out.println((end-start)/1000+"秒,"+(end-start)+"毫秒");
        }
    }

    public static void main(String[] args) throws IOException {
        File f1 = new File("E:\\QQ文件\\需求清单1.0.doc");
        File f2 = new File("J:\\测试第一种方法.doc");
        File f3 = new File("E:\\QQ文件\\需求清单1.0.doc");
        File f4 = new File("J:\\测试第二种方法.doc");
        File f5 = new File("E:\\QQ文件\\需求清单1.0.doc");
        File f6 = new File("J:\\测试第三种方法.doc");
        copyFile1(f1,f2);
        copyFile2(f3,f4);
        copyFile3(f5,f6);
    }
}

 

package com.timeschedule.timeschedule.test;

import java.io.*;
import java.nio.channels.FileChannel;

public class FileCopyMethods {

/**
* 文件输入输出流
*/

public static void copyFile1(File f1,File f2) throws IOException {
if (!f2.exists()){
f2.mkdirs();
f2.createNewFile();
}
f2.setWritable(true);
long start = System.currentTimeMillis();
InputStream in = null;
OutputStream out = null;
try{
in= new BufferedInputStream(new FileInputStream(f1));
out=new BufferedOutputStream(new FileOutputStream(f2));
byte[] b=new byte[1024];
int flag=0;
while((flag=in.read(b))!=-1){
out.write(b, 0, flag);
}
}catch (IOException ex){

}finally {
if (in!=null){
in.close();
}
if (out!=null){
out.close();
}
long end = System.currentTimeMillis();
System.out.println((end-start)/1000+","+(end-start)+"毫秒");
}
}

public static void copyFile2(File f1,File f2) throws IOException {
if (!f2.exists()){
f2.mkdirs();
f2.createNewFile();
}
f2.setWritable(true);
long start = System.currentTimeMillis();
FileInputStream fi = null;
FileOutputStream fo = null;
FileChannel in = null;
FileChannel out = null;
try {
fi = new FileInputStream(f1);
fo = new FileOutputStream(f2);
in = fi.getChannel();//得到对应的文件通道
out = fo.getChannel();//得到对应的文件通道
in.transferTo(0, in.size(), out);//连接两个通道,并且从in通道读取,然后写入out通道
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
fi.close();
in.close();
fo.close();
out.close();
} catch (IOException e) {
e.printStackTrace();
}
long end = System.currentTimeMillis();
System.out.println((end-start)/1000+","+(end-start)+"毫秒");
}
}

public static void copyFile3(File f1,File f2) throws IOException {
if (!f2.exists()){
f2.mkdirs();
f2.createNewFile();
}
f2.setWritable(true);
long start = System.currentTimeMillis();
FileInputStream fileInputStream = null;
FileOutputStream fileOutputStream = null;
try{
fileInputStream = new FileInputStream(f1);
fileOutputStream =new FileOutputStream(f2);
byte[] b=new byte[1024];
int flag=0;
while((flag=fileInputStream.read(b))!=-1){
fileOutputStream.write(b, 0, flag);
}
}catch (IOException ex){

}finally {
if (fileInputStream!=null){
fileInputStream.close();
}
if (fileOutputStream!=null){
fileOutputStream.close();
}
long end = System.currentTimeMillis();
System.out.println((end-start)/1000+","+(end-start)+"毫秒");
}
}

public static void main(String[] args) throws IOException {
File f1 = new File("E:\\QQ文件\\需求清单1.0.doc");
File f2 = new File("J:\\测试第一种方法.doc");
File f3 = new File("E:\\QQ文件\\需求清单1.0.doc");
File f4 = new File("J:\\测试第二种方法.doc");
File f5 = new File("E:\\QQ文件\\需求清单1.0.doc");
File f6 = new File("J:\\测试第三种方法.doc");
copyFile1(f1,f2);
copyFile2(f3,f4);
copyFile3(f5,f6);
}
}
posted @ 2021-01-12 11:25  DreamCatt  阅读(79)  评论(0)    收藏  举报