File content[] = new File("C:/FilesToGo/").listFiles(); for (int i = 0; i < content.length; i++){ String destiny = "C:/Kingdoms/"+content[i].getName(); File desc = new File(destiny); try { Files.copy(content[i].toPath(), desc.toPath(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } }
This is what I have. It copies everything just fine. But among the contents there are some folders. The folders are copied but the folder’s contents are not.
Advertisement
Answer
Recursion. Here is a method the uses rescursion to delete a system of folders:
public void move(File file, File targetFile) { if(file.isDirectory() && file.listFiles() != null) { for(File file2 : file.listFiles()) { move(file2, new File(targetFile.getPath() + "\" + file.getName()); } } try { Files.copy(file, targetFile.getPath() + "\" + file.getName(), StandardCopyOption.REPLACE_EXISTING); } catch (IOException e) { e.printStackTrace(); } }
Didn’t test the code, but it should work. Basically, it digs down into the folders, telling it to move the item, if its a folder, go through all its children, and move them, etc.