diff --git a/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/ApacheCommons.java b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/ApacheCommons.java new file mode 100644 index 0000000000..67af0594a4 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/ApacheCommons.java @@ -0,0 +1,15 @@ +package com.baeldung.copyfolder; + +import java.io.File; +import java.io.IOException; + +import org.apache.commons.io.FileUtils; + +public class ApacheCommons { + + public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException { + File sourceFolder = new File(sourceDirectoryLocation); + File destinationFolder = new File(destinationDirectoryLocation); + FileUtils.copyDirectory(sourceFolder, destinationFolder); + } +} diff --git a/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/CoreOld.java b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/CoreOld.java new file mode 100644 index 0000000000..d1a0158db3 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/CoreOld.java @@ -0,0 +1,38 @@ +package com.baeldung.copyfolder; + +import java.io.File; +import java.io.FileInputStream; +import java.io.FileOutputStream; +import java.io.IOException; +import java.io.InputStream; +import java.io.OutputStream; + +public class CoreOld { + + public static void copyDirectoryJavaUnder7(File source, File destination) throws IOException { + if (source.isDirectory()) { + copyDirectory(source, destination); + } else { + copyFile(source, destination); + } + } + + private static void copyDirectory(File sourceFolder, File destinationFolder) throws IOException { + if (!destinationFolder.exists()) { + destinationFolder.mkdir(); + } + for (String f : sourceFolder.list()) { + copyDirectoryJavaUnder7(new File(sourceFolder, f), new File(destinationFolder, f)); + } + } + + private static void copyFile(File sourceFile, File destinationFile) throws IOException { + try (InputStream in = new FileInputStream(sourceFile); OutputStream out = new FileOutputStream(destinationFile)) { + byte[] buf = new byte[1024]; + int length; + while ((length = in.read(buf)) > 0) { + out.write(buf, 0, length); + } + } + } +} diff --git a/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/JavaNio.java b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/JavaNio.java new file mode 100644 index 0000000000..368118a943 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/main/java/com/baeldung/copyfolder/JavaNio.java @@ -0,0 +1,22 @@ +package com.baeldung.copyfolder; + +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; + +public class JavaNio { + + public static void copyDirectory(String sourceDirectoryLocation, String destinationDirectoryLocation) throws IOException { + Files.walk(Paths.get(sourceDirectoryLocation)) + .forEach(a -> { + Path b = Paths.get(destinationDirectoryLocation, a.toString() + .substring(sourceDirectoryLocation.length())); + try { + Files.copy(a, b); + } catch (IOException e) { + e.printStackTrace(); + } + }); + } +} diff --git a/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/ApacheCommonsUnitTest.java b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/ApacheCommonsUnitTest.java new file mode 100644 index 0000000000..71e4c52104 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/ApacheCommonsUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.copyfolder; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Comparator; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class ApacheCommonsUnitTest { + + private final String sourceFolderLocation = "src/test/resources/sourceFolder"; + private final String subFolderName = "/childFolder"; + private final String fileName = "/file.txt"; + private final String destinationFolderLocation = "src/test/resources/destinationFolder"; + + @BeforeEach + public void createFolderWithSubfolderAndFile() throws IOException { + Files.createDirectories(Paths.get(sourceFolderLocation)); + Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName)); + Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName)); + } + + @Test + public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException { + ApacheCommons.copyDirectory(sourceFolderLocation, destinationFolderLocation); + + assertTrue(new File(destinationFolderLocation).exists()); + assertTrue(new File(destinationFolderLocation + subFolderName).exists()); + assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists()); + } + + @Test + public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() { + assertThrows(Exception.class, () -> ApacheCommons.copyDirectory("nonExistingFolder", destinationFolderLocation)); + } + + @AfterEach + public void cleanUp() throws IOException { + Files.walk(Paths.get(sourceFolderLocation)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + if (new File(destinationFolderLocation).exists()) { + Files.walk(Paths.get(destinationFolderLocation)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } + } + +} diff --git a/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/CoreOldUnitTest.java b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/CoreOldUnitTest.java new file mode 100644 index 0000000000..ba45fd3cd0 --- /dev/null +++ b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/CoreOldUnitTest.java @@ -0,0 +1,63 @@ +package com.baeldung.copyfolder; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Comparator; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class CoreOldUnitTest { + + private final String sourceFolderLocation = "src/test/resources/sourceFolder"; + private final String subFolderName = "/childFolder"; + private final String fileName = "/file.txt"; + private final String destinationFolderLocation = "src/test/resources/destinationFolder"; + + @BeforeEach + public void createFolderWithSubfolderAndFile() throws IOException { + Files.createDirectories(Paths.get(sourceFolderLocation)); + Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName)); + Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName)); + } + + @Test + public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException { + File sourceFolder = new File(sourceFolderLocation); + File destinationFolder = new File(destinationFolderLocation); + CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder); + + assertTrue(new File(destinationFolderLocation).exists()); + assertTrue(new File(destinationFolderLocation + subFolderName).exists()); + assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists()); + } + + @Test + public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() throws IOException { + File sourceFolder = new File("nonExistingFolder"); + File destinationFolder = new File(destinationFolderLocation); + assertThrows(IOException.class, () -> CoreOld.copyDirectoryJavaUnder7(sourceFolder, destinationFolder)); + } + + @AfterEach + public void cleanUp() throws IOException { + Files.walk(Paths.get(sourceFolderLocation)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + if (new File(destinationFolderLocation).exists()) { + Files.walk(Paths.get(destinationFolderLocation)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } + } + +} diff --git a/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/JavaNioUnitTest.java b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/JavaNioUnitTest.java new file mode 100644 index 0000000000..162811912c --- /dev/null +++ b/core-java-modules/core-java-io-3/src/test/java/com/baeldung/copyfolder/JavaNioUnitTest.java @@ -0,0 +1,59 @@ +package com.baeldung.copyfolder; + +import static org.junit.Assert.assertTrue; +import static org.junit.jupiter.api.Assertions.assertThrows; + +import java.io.File; +import java.io.IOException; +import java.nio.file.Files; +import java.nio.file.Path; +import java.nio.file.Paths; +import java.util.Comparator; + +import org.junit.jupiter.api.AfterEach; +import org.junit.jupiter.api.BeforeEach; +import org.junit.jupiter.api.Test; + +public class JavaNioUnitTest { + + private final String sourceFolderLocation = "src/test/resources/sourceFolder"; + private final String subFolderName = "/childFolder"; + private final String fileName = "/file.txt"; + private final String destinationFolderLocation = "src/test/resources/destinationFolder"; + + @BeforeEach + public void createFolderWithSubfolderAndFile() throws IOException { + Files.createDirectories(Paths.get(sourceFolderLocation)); + Files.createDirectories(Paths.get(sourceFolderLocation + subFolderName)); + Files.createFile(Paths.get(sourceFolderLocation + subFolderName + fileName)); + } + + @Test + public void whenSourceFolderExists_thenFolderIsFullyCopied() throws IOException { + JavaNio.copyDirectory(sourceFolderLocation, destinationFolderLocation); + + assertTrue(new File(destinationFolderLocation).exists()); + assertTrue(new File(destinationFolderLocation + subFolderName).exists()); + assertTrue(new File(destinationFolderLocation + subFolderName + fileName).exists()); + } + + @Test + public void whenSourceFolderDoesNotExist_thenExceptionIsThrown() { + assertThrows(IOException.class, () -> JavaNio.copyDirectory("nonExistingFolder", destinationFolderLocation)); + } + + @AfterEach + public void cleanUp() throws IOException { + Files.walk(Paths.get(sourceFolderLocation)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + if (new File(destinationFolderLocation).exists()) { + Files.walk(Paths.get(destinationFolderLocation)) + .sorted(Comparator.reverseOrder()) + .map(Path::toFile) + .forEach(File::delete); + } + } + +}