话不多说直接上代码
public void copyFolder(List<String> files, String oldPath, String newPath) {
try {
File mFile = new File(newPath);
if (!mFile.exists()) {
mFile.mkdirs();
}
String[] file = files.toArray(new String[files.size()]);
File temp = null;
for (int i = 0; i < file.length; i++) {
if (oldPath.endsWith(File.separator)) {
temp = new File(oldPath + file[i]);
} else {
temp = new File(oldPath + File.separator + file[i]);
}
if (temp.isFile()) {
String fileName = newPath + File.separator + (temp.getName()).toString();
File testFile = new File(fileName);
if (!testFile.exists()) {
FileInputStream input = new FileInputStream(temp);
FileOutputStream output = new FileOutputStream(fileName);
byte[] b = new byte[1024 * 5];
int len;
while ((len = input.read(b)) != -1) {
output.write(b, 0, len);
}
output.flush();
output.close();
input.close();
}
}
if (temp.isDirectory()) {
List<String> fileListTwo = Arrays.asList(temp.list());
copyFolder(fileListTwo, oldPath + File.separator + file[i], newPath + File.separator + file[i]);
}
}
} catch (Exception e) {
System.out.println("复制整个文件夹内容操作出错");
e.printStackTrace();
}
}
有什么不对的地方欢迎指正(如果对您有帮助,可以鼓励鼓励博主,一毛也是爱)