关于文件拷贝你会了吗

来源:互联网 发布:页面间如何传递数据 编辑:程序博客网 时间:2024/04/27 21:58

在真正的编程中,很多时候我们会遇到一些关于文件拷贝的问题!

下面是自己写的一个实例,希望对各位有用!!!

 


import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.channels.FileChannel;
import java.util.Date;

public class copyFile {

    public static void main(String[] args) {

        String path = "d://src";
        String tow = "d://copy//";
        File f = new File(path);
        File copy = new File(tow);
        copyFile c = new copyFile();
        File cf = c.testFile(f);
        try {
            long time = forTransfer(cf, copy);
            System.out.println("拷贝文件花费时间:"+time+"毫秒");
        } catch (Exception e) {
            System.out.println("拷贝出错了");
        }
    }

    public File testFile(File dir) {
        if (dir.exists()) {
            if (dir.isDirectory()) {
                System.out.println("目录已经存在无需创建");
            }
        } else {
            try {
                dir.mkdirs();
                System.out.println("目录创建成功");
            } catch (Exception ef) {
                System.out.println("目录创建出错");
            }
        }
        File f = new File(dir.getPath(), "file.txt");
        if (f.exists()) {
            System.out.println("文件已经存在" + f.getAbsolutePath());
        } else {
            try {
                f.createNewFile();
                RandomAccessFile raf = new RandomAccessFile(dir.getPath()
                        + "//" + f.getName(), "rw");
                raf.write("拷贝我吧,给你自由".getBytes());
                raf.close();
                System.out.println("需要拷贝的文件创建成功:" + f.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return f;
    }

    public static long forTransfer(File f1, File f2) throws Exception {
        String Name = null;
        String path = f1.getPath();
        System.out.println("源文件所在位置:" + path);
        // 获得需要拷贝的文件的名称
        int a = path.lastIndexOf("//");
        if ((a > 0) && (a < (path.length() - 1)))
            Name = path.substring(a + 1);
        String fileName = "Copy"+Name;
        System.out.println("您当前拷贝的文件:" + fileName);
        System.out.println(f2.getAbsolutePath()+"kankna");
        File f = new File(f2, fileName);
        if (!f2.exists()){
            try {
                f2.mkdirs();
            } catch (Exception ef) {
                System.out.println("目录创建出错");
            }
        }
        if (f.exists()) {
            System.out.println("文件已经存在:" + f.getAbsolutePath());
        } else {
            try {
                f.createNewFile();
                System.out.println("文件拷贝成功:"+f.getAbsolutePath());
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        long time = new Date().getTime();
        int length = 2097152;
        FileInputStream in = new FileInputStream(f1);
        FileOutputStream out = new FileOutputStream(f);
        FileChannel inC = in.getChannel();
        FileChannel outC = out.getChannel();
        int i = 0;
        while (true) {
            if (inC.position() == inC.size()) {
                inC.close();
                outC.close();
                return new Date().getTime() - time;
            }
            if ((inC.size() - inC.position()) < 20971520)
                length = (int) (inC.size() - inC.position());
            else
                length = 20971520;
            inC.transferTo(inC.position(), length, outC);
            inC.position(inC.position() + length);
            i++;
        }
    }

}