java中文件夹复制递归_JAVA-NIO.2:递归复制文件目录、文件、子目录

//通过使用NIO.2实现递归复制文件目录及文件、子目录,用到了SimpleFileVisitor.

//​修改方法中的内容,可实现搜索特定目录下的文件或文件内容。如文件名为一特定值,则反馈文件路径,并结束搜索;递归的加载文件内容,比较,并返回结果。

​//List list =

Files.readAllLines(file);

//if (list.toString().indexOf(keyWord) != -1) {

//System.out.println("file.toAbsolutePath():" +

file.toAbsolutePath().toString());

//list.forEach(obj ->

System.out.println(obj));

//}

//this way can be used to seach key words for small text files;

if big file, can use lucene.

import java.io.File;

import java.io.IOException;

import java.nio.file.FileVisitResult;

import java.nio.file.Files;

import java.nio.file.Path;

import java.nio.file.Paths;

import java.nio.file.SimpleFileVisitor;

import java.nio.file.StandardCopyOption;

import

java.nio.file.attribute.BasicFileAttributes;

//Copy file and directory from source to target

recursively using NIO.2

public class CopyFileAndDirectoryRecursivelyUsingNIO2

{

private static String sourcePath =

"D:\\brightmart\\myEclipseWorkspace\\javaCrazy";

private static String targetPath =

"D:\\brightmart\\copy_temp";

public static void main(String[] args) throws Exception {

CopyFileAndDirectoryRecursivelyUsingNIO2 P_X_Test1_ListSubFiles

= new CopyFileAndDirectoryRecursivelyUsingNIO2();

P_X_Test1_ListSubFiles.listSubFileX(sourcePath, targetPath);

}

// use SimpleFileVisitor to copy file and

folders(including all sub folders and sub folder's

file)

public void listSubFileX(String pathName, String

targetString) throws Exception {

Path startPath = Paths.get(pathName);

Path target = Paths.get(targetString);

File fileTarget = target.toFile();

if (!fileTarget.exists()) {

fileTarget.mkdirs();

}

Files.walkFileTree(startPath, new SimpleFileVisitor() {

@Override

public FileVisitResult visitFile(Path file,

BasicFileAttributes attrs) throws IOException {

String fileAbsPath = file.toAbsolutePath().toString();

String subStringOfSource =

fileAbsPath.substring(fileAbsPath.indexOf(pathName) +

pathName.length());

File tempFile = new File(fileTarget,

subStringOfSource);//construct targetFile by combine

Path target = tempFile.toPath();

Files.copy(file, target,

StandardCopyOption.REPLACE_EXISTING);// copy file to target,

replace if existing.

return FileVisitResult.CONTINUE;

}

@Override

public FileVisitResult preVisitDirectory(Path dir,

BasicFileAttributes attrs) throws IOException {

String dirAbsPath = dir.toAbsolutePath().toString();

String subStringOfSource =

dirAbsPath.substring(dirAbsPath.indexOf(pathName) +

pathName.length());

File tempFile = new File(fileTarget, subStringOfSource);

if (!tempFile.exists() &&

!pathName.equals(subStringOfSource)) {// create directory if not

exists.

tempFile.mkdirs();

}

return FileVisitResult.CONTINUE;

}

});

}

}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值