首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 开发语言 > 编程 >

java copy 索引 文件 其它写法

2012-10-28 
java copy 目录 文件 其它写法public static void copyDir(final File src, final File dest) throws IOEx

java copy 目录 文件 其它写法
public static void copyDir(final File src, final File dest) throws IOException {
        dest.mkdirs();
        File[] files = src.listFiles();

        int j = files.length;   // cache the length so it doesn't need to be looked up over and over in the loop
        for (int i = 0; i < j; i++) {
            File file = files[i];
            if (file.isDirectory()) {
                copyDir(file, new File(dest, file.getName()));
            } else {
                copyFile(file, new File(dest, file.getName()));
            }
        }
    }

public static void copyFile(final File src, final File dest) throws IOException {
        dest.getParentFile().mkdirs();
        dest.createNewFile();

        FileChannel sourceChannel = new FileInputStream(src).getChannel();
        FileChannel targetChannel = new FileOutputStream(dest).getChannel();
        sourceChannel.transferTo(0, sourceChannel.size(), targetChannel);
        sourceChannel.close();
        targetChannel.close();
    }

热点排行