大文件复制 java_java复制整个文件夹(对大文件的操作)

package xy;

import java.io.File;

import java.io.FileFilter;

import java.io.FileInputStream;

import java.io.FileNotFoundException;

import java.io.FileOutputStream;

import java.io.IOException;

import java.io.InputStream;

import java.io.OutputStream;

import java.util.ArrayList;

import java.util.List;

public class FileUtils {

private static final int DEFAULT_BUFFER_SIZE

= 1024 * 4;

//调用函数

public static void copyDirectory(File

srcDir, File destDir) throws IOException {

copyDirectory(srcDir, destDir, true);

}

public static void copyDirectory(File

srcDir, File destDir, boolean preserveFileDate) throws IOException

{

copyDirectory(srcDir, destDir, null,

preserveFileDate);

}

public static void copyDirectory(File

srcDir, File destDir, FileFilter filter) throws IOException {

copyDirectory(srcDir, destDir, filter,

true);

}

//开始复制目录

public static void copyDirectory(File

srcDir, File destDir, FileFilter filter, boolean preserveFileDate)

throws IOException {

if (srcDir == null) {

throw new

NullPointerException("Source must not be null");

}

if (destDir == null) {

throw new

NullPointerException("Destination must not be null");

}

if (srcDir.exists() == false) {

throw new

FileNotFoundException("Source '" + srcDir + "' does not

exist");

}

if (srcDir.isDirectory() == false) {

throw new

IOException("Source '" + srcDir + "' exists but is not a

directory");

}

if

(srcDir.getCanonicalPath().equals(destDir.getCanonicalPath()))

{

throw new

IOException("Source '" + srcDir + "' and destination '" + destDir +

"' are the same");

}

// Cater for destination being directory

within the source directory (see IO-141)

List exclusionList = null;

if

(destDir.getCanonicalPath().startsWith(srcDir.getCanonicalPath()))

{

File[] srcFiles = filter

== null ? srcDir.listFiles() : srcDir.listFiles(filter);

if (srcFiles != null

&& srcFiles.length >

0) {

exclusionList = new

ArrayList(srcFiles.length);

for (int i = 0; i

< srcFiles.length; i++) {

File

copiedFile = new File(destDir, srcFiles[i].getName());

exclusionList.add(copiedFile.getCanonicalPath());

}

}

}

doCopyDirectory(srcDir, destDir, filter,

preserveFileDate, exclusionList);

}

//复制目录

private static void doCopyDirectory(File

srcDir, File destDir, FileFilter filter, boolean preserveFileDate,

List exclusionList) throws IOException {

if (destDir.exists()) {

if (destDir.isDirectory()

== false) {

throw new

IOException("Destination '" + destDir + "' exists but is not a

directory");

}

} else {

if (destDir.mkdirs() ==

false) {

throw new

IOException("Destination '" + destDir + "' directory cannot be

created");

}

if (preserveFileDate)

{

destDir.setLastModified(srcDir.lastModified());

}

}

if (destDir.canWrite() == false) {

throw new

IOException("Destination '" + destDir + "' cannot be written

to");

}

// recurse

File[] files = filter == null ?

srcDir.listFiles() : srcDir.listFiles(filter);

if (files == null) { // null if security

restricted

throw new

IOException("Failed to list contents of " + srcDir);

}

for (int i = 0; i <

files.length; i++) {

File copiedFile = new

File(destDir, files[i].getName());

if (exclusionList == null

|| !exclusionList.contains(files[i].getCanonicalPath())) {

if

(files[i].isDirectory()) {

doCopyDirectory(files[i], copiedFile, filter,

preserveFileDate, exclusionList);

} else {

doCopyFile(files[i], copiedFile,

preserveFileDate);

}

}

}

}

//开始复制

private static void doCopyFile(File srcFile,

File destFile, boolean preserveFileDate) throws IOException {

if (destFile.exists()

&& destFile.isDirectory()) {

throw new

IOException("Destination '" + destFile + "' exists but is a

directory");

}

FileInputStream input = new

FileInputStream(srcFile);

try {

FileOutputStream output =

new FileOutputStream(destFile);

try {

copy(input,

output);

} finally {

try {

output.close();

} catch (IOException

ioe) {

// do

nothing

}

}

} finally {

try {

input.close();

} catch (IOException ioe)

{

// do nothing

}

}

if (srcFile.length() != destFile.length())

{

throw new

IOException("Failed to copy full contents from '" + srcFile + "' to

'" + destFile + "'");

}

if (preserveFileDate) {

destFile.setLastModified(srcFile.lastModified());

}

}

//测试出要复制的文件数量

public static int copy(InputStream input,

OutputStream output) throws IOException {

long count = copyLarge(input,

output);

if (count >

Integer.MAX_VALUE) {

return -1;

}

return (int) count;

}

//复制大文件

public static long copyLarge(InputStream

input, OutputStream output) throws IOException {

byte[] buffer = new

byte[DEFAULT_BUFFER_SIZE];

long count = 0;

int n = 0;

while (-1 != (n = input.read(buffer)))

{

output.write(buffer, 0,

n);

count += n;

}

return count;

}

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值