java实现文件夹的拷贝功能_java实现文件以及文件夹拷贝的几种方式

/**

* 支持jdk7以上

* @param source

* @param target

*/

public static void copyFileJ(String source,String target) {

File fileFile=new File(source);

if (!fileFile.isFile()) {

return;

}

File targetFile=new File(target);

try {

Files.copy(fileFile.toPath(), targetFile.toPath());

} catch (IOException e) {

e.printStackTrace();

}

}

/**

* 基于BIO传统的文件拷贝

* @param source

* @param target

*/

public static void copyBio(String source,String target) {

File sourceFile=new File(source);

File targetFile=new File(target);

copyFileBio(sourceFile, targetFile);

}

public static void copyFileBio(File sourceFile,File targetFile) {

try {

BufferedInputStream inputStream=new BufferedInputStream(new FileInputStream(sourceFile));

BufferedOutputStream outputStream=new BufferedOutputStream(new FileOutputStream(targetFile));

byte[] bs=new byte[1024];

int count;

while((count=inputStream.read(bs))!=-1) {

outputStream.write(bs, 0, count);

}

inputStream.close();

outputStream.close();

} catch (Exception e) {

e.printStackTrace();

}

}

/**

* 基于Nio的文件拷贝 每次拷贝文件的一小块

* @param source

* @param target

*/

public static void copyNioBuffer(String source,String target) {

try {

RandomAccessFile sourceFile=new RandomAccessFile(source, "rw");

FileChannel sourceChannel=sourceFile.getChannel();

RandomAccessFile targetFile=new RandomAccessFile(target, "rw");

FileChannel targetChannl=targetFile.getChannel();

ByteBuffer buffer=ByteBuffer.allocate(1024);

int byteRead=sourceChannel.read(buffer);

while(byteRead != -1) {

buffer.flip();

targetChannl.write(buffer);

buffer.compact();

byteRead=sourceChannel.read(buffer);

}

targetChannl.close();

sourceChannel.close();

targetFile.close();

sourceFile.close();

} catch (IOException e) {

// TODO Auto-generated catch block

e.printStackTrace();

}

}

public static void copyNio(String source,String target) {

try {

RandomAccessFile sourceFile=new RandomAccessFile(source, "rw");

FileChannel sourceChannel=sourceFile.getChannel();

RandomAccessFile targetFile=new RandomAccessFile(target, "rw");

FileChannel targetChannel=targetFile.getChannel();

long position=0;

long count=sourceChannel.size();

targetChannel.transferFrom(sourceChannel, position, count);

sourceChannel.close();

targetChannel.close();

sourceFile.close();

targetFile.close();

} catch ( IOException e) {

e.printStackTrace();

}

}

/**

* 文件夹的拷贝

* @param source

* @param target

*/

public static void copyDir(String source,String target) {

if (!target.endsWith(File.separator)) {

target+=File.separator;

}

File file=new File(source);

if (file.isDirectory()) {

recursionDir(file,target);

}else {

target+=file.getName();

File targetFile=new File(target);

copyFileBio(file, targetFile);

}

}

private static void recursionDir(File fileSource,String base) {

if (fileSource.isDirectory()) {

File[] files=fileSource.listFiles();

if (files.length>0) {

for(File file:files) {

String basetemp=base+File.separator+file.getName();

recursionDir(file,basetemp);

}

}

}else {

File targetFile=new File(base);

File fileParent=targetFile.getParentFile();

if (!fileParent.exists()) {

fileParent.mkdirs();

}

copyFileBio(fileSource, targetFile);

}

}

/**

* 文件以及文件夹的删除

* @param file

*/

public static void deleteFileDir(File file) {

if (file.exists()) {

if (file.isDirectory()) {

File[] files=file.listFiles();

for(File f:files) {

deleteFileDir(f);

}

}

file.delete();

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值