diff --git a/src/Main.java b/src/Main.java index aedc0fa..89873e6 100644 --- a/src/Main.java +++ b/src/Main.java @@ -1,17 +1,22 @@ -import java.io.*; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.List; +import java.util.stream.Collectors; public class Main { public static void main(String[] args) { - File fromFolder = new File("c:/AmericasCardroom"); - File destFolder = new File("c:/AmericasCardroom/new"); + Path fromFolder = Paths.get("c:/AmericasCardroom"); + Path destFolder = Paths.get("c:/AmericasCardroom/new"); - if (!fromFolder.exists()) { + if (!fromFolder.toFile().exists()) { System.out.println("Исходная директория не существует"); System.exit(0); } else { try { copyFolder(fromFolder, destFolder); - } catch (IOException e) { + } catch (Exception e) { e.printStackTrace(); System.exit(0); } @@ -19,37 +24,18 @@ public static void main(String[] args) { System.out.println("Done"); } - public static void copyFolder(File fromDir, File toDir) throws IOException { + public static void copyFolder(Path fromDir, Path toDir) throws Exception { - if (fromDir.isDirectory()) { - if (!toDir.exists()) { - toDir.mkdir(); - if (!toDir.exists()) { - throw new IOException("Вы должны обладать правами администратора для записи новых файлов"); - } - System.out.println("Directory copied from " - + fromDir + " to " + toDir); - } - String files[] = fromDir.list(); + List paths = Files.walk(fromDir).collect(Collectors.toList()); - for (String file : files) { - File srcFile = new File(fromDir, file); - File destFile = new File(toDir, file); - if (!destFile.getParentFile().equals(srcFile)) { - copyFolder(srcFile, destFile); - } - } - } else { - InputStream in = new FileInputStream(fromDir); - OutputStream out = new FileOutputStream(toDir); - byte[] buffer = new byte[1024]; - int length; - while ((length = in.read(buffer)) > 0) { - out.write(buffer, 0, length); - } - in.close(); - out.close(); - System.out.println("File copied from " + fromDir + " to " + toDir); - } + paths.stream().filter(path -> !toDir.relativize(path).toString().equals("")) + .forEach(path -> + { + try { + Files.copy(path, Paths.get(path.toString().replace(fromDir.toString(), toDir.toString()))); + } catch (IOException e) { + e.printStackTrace(); + } + }); } -} \ No newline at end of file +}