copy directory to another directory java code example

Example: how to copy all files and subdirectories in directory in java

public static void storeFile(String path, File file) {
		try {
			File newFile = new File(path);
			newFile.createNewFile();
			Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	public static void storeDirectory(String path, File file) {
		new File(path).mkdir();
		
		for (File files : file.listFiles()) {
			if (files.isDirectory())
				storeDirectory(path + "/" + files.getName(), files);
			else
				storeFile(path + "/" + files.getName(), files);
		}
	}

Tags:

Java Example