how to copy all files and subdirectories in directory in java

Solutions on MaxInterview for how to copy all files and subdirectories in directory in java by the best coders in the world

showing results for - "how to copy all files and subdirectories in directory in java"
Valentín
02 Jan 2019
1	public static void storeFile(String path, File file) {
2		try {
3			File newFile = new File(path);
4			newFile.createNewFile();
5			Files.copy(file.toPath(), newFile.toPath(), StandardCopyOption.REPLACE_EXISTING);
6		} catch (Exception e) {
7			e.printStackTrace();
8		}
9	}
10	
11	public static void storeDirectory(String path, File file) {
12		new File(path).mkdir();
13		
14		for (File files : file.listFiles()) {
15			if (files.isDirectory())
16				storeDirectory(path + "/" + files.getName(), files);
17			else
18				storeFile(path + "/" + files.getName(), files);
19		}
20	}