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();
    }

你可能感兴趣的:(java,cache,J#,UP)