直接复制下面代码即可,可直接使用
public static void moveFile(String orgin_path, String moved_path) {
File[] files = new File(orgin_path).listFiles();
for (File file : files) {
if (file.isFile()) {// 如果是文件
File movedFile = new File(moved_path + File.separator + file.getName());
// 如果文件已经存在则跳过
if (movedFile.exists()) {
System.out.println("文件已经存在1:" + file.getAbsolutePath());
System.out.println("文件已经存在2:" + movedFile.getAbsolutePath());
continue;
} else {
// 否则复制
try {
FileUtil.copyFile(file, movedFile);
} catch (Exception e) {
e.printStackTrace();
}
}
}
if (file.isDirectory()) {// 如果是目录,就递归
String childMPath = moved_path + File.separator + file.getName();
new File(childMPath).mkdir();
moveFile(file.getAbsolutePath(), childMPath);
}
}
}
public static void copyFile(File resource, File target) throws Exception {
FileInputStream inputStream = new FileInputStream(resource);
BufferedInputStream bufferedInputStream = new BufferedInputStream(inputStream);
FileOutputStream outputStream = new FileOutputStream(target);
BufferedOutputStream bufferedOutputStream = new BufferedOutputStream(outputStream);
byte[] bytes = new byte[1024 * 2];
int len = 0;
while ((len = inputStream.read(bytes)) != -1) {
bufferedOutputStream.write(bytes, 0, len);
}
bufferedOutputStream.flush();
bufferedInputStream.close();
bufferedOutputStream.close();
inputStream.close();
outputStream.close();
long end = System.currentTimeMillis();
}
网友评论