1 问题
java在执行某个迭代方法时,有时给予的某些参数会在迭代过程中发生变化,但是迭代结束又需要用到这些参数的原始值
2 思路
定义一个布尔值,在迭代开始之前做判断,该布尔值为true时记住这个原始路径
3 代码实现
public class CommoTask {
//@Setter
private String rootPath;
boolean b;
//拿出文件
public void decideFileDir(String path, String savepath, String regularTypes,
FileTransferServiceImpl fileTransferService, IAchivefailFileService achivefailFileService) {
if (!b) {
this.rootPath = path;
b = true;
}
File file = new File(path);
try {
// 如果这个路径是文件夹
if (file.isDirectory()) {
File[] files = file.listFiles();
for (int i = 0; i < Objects.requireNonNull(files).length; i++) {
// 如果还是文件夹 递归获取里面的文件 文件夹
if (files[i].isDirectory()) {
decideFileDir(files[i].getPath(), savepath, regularTypes, fileTransferService, achivefailFileService);
} else {
File oneFile = new File(files[i].getPath());
achiveFile(oneFile, rootPath, savepath, regularTypes, fileTransferService, achivefailFileService);
}
}
} else {
achiveFile(file, path, savepath, regularTypes, fileTransferService, achivefailFileService);
}
} catch (Exception e) {
log.error("文件搬运报错", e);
}
}
}