import java.io.File;
public class FileMove {
/**
* Move all files in the specified folder
*
* @param fromDir
* Directory of files to be moved
* @param toDir
* Target file directory
* @throwsException
*/
public static void fileMove(String from, String to) throws Exception {
try {
File dir = new File(from);
// file list
File[] files = dir.listFiles();
if (files == null)
return;
// Target
File moveDir = new File(to);
if (!moveDir.exists()) {
moveDir.mkdirs();
}
//File move
for (int i = 0; i < files.length; i++) {
if (files[i].isDirectory()) {
fileMove(files[i].getPath(), to + "//" + files[i].getName());
// Success, delete the original file
files[i].delete();
}
File moveFile = new File(moveDir.getPath() + "//"
+ files[i].getName());
// If it exists in the target folder, delete it.
if (moveFile.exists()) {
moveFile.delete();
}
files[i].renameTo(moveFile);
}
} catch (Exception e) {
throw e;
}
}
}