// If targetLocation does not exist, it will be created. public static void copyDirectory(File sourceLocation, File targetLocation) throws IOException { System.out.println(sourceLocation.getName() + " - " + targetLocation.getName()); if (sourceLocation.isDirectory()) { if (FileTree.isFolderAccepted(sourceLocation.getName())) { if (!targetLocation.exists()) { targetLocation.mkdirs(); } String[] children = sourceLocation.list(); for (int i = 0; i < children.length; i++) { copyDirectory( new File(sourceLocation, children[i]), new File(targetLocation, children[i])); } } } else { InputStream in = new FileInputStream(sourceLocation); OutputStream out = new FileOutputStream(targetLocation); // Copy the bits from instream to outstream byte[] buf = new byte[1024]; int len; while ((len = in.read(buf)) > 0) { out.write(buf, 0, len); } in.close(); out.close(); } }
public static void backupCompleteProject() { File mainDir = mainFrame.getFileTree().getMainDir(); try { copyDirectory(mainDir, getTargetLocation()); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } }